Is there an easy way to do a one time template string compile/one way data bind? I do not need dynamic components just something that would process template string e.g.My name is {{person.name}}
and bind a supplied context e.g. {name: 'John'}
.
Asked
Active
Viewed 116 times
2
-
1http://stackoverflow.com/questions/34784778/equivalent-of-compile-in-angular-2/37044960#37044960 – Günter Zöchbauer Jan 16 '17 at 15:46
-
@GünterZöchbauer this is exactly what I do not want to do - create new module, create dynamic component, etc This is an overkill for a simple task that I want to achieve. – sdev Jan 16 '17 at 17:29
-
I don't think it will change a lot if you want that or not ;-) – Günter Zöchbauer Jan 16 '17 at 17:33
1 Answers
0
If you use babeljs or similar (or specific browsers) you can use Template literals, like this:
var person = { name: 'john' };
var result = `My name is ${person.name}`;
console.log(result);
If not, you can use a regex like this:
var person = { name: 'john' };
var result = 'My name is {{person.name}}'.replace(/{{?.*}}/, function(a) {
return eval(a);
});
console.log(result);
Of course that it's dummy demo but this is the principal.

Mosh Feu
- 28,354
- 16
- 88
- 135