String interpolation in Angular 2.0

I have a line like {name} is my name. Greeting {sender}

is there any module in angualar 2.0 so that i can do something like string.format()this in c #?

I know this can be done using the custom vanila js method, but I want to know if there is any module inside angular 2 to handle this. They use pattern interpolation, so how to do it with a normal string

+4
source share
3 answers

Check out ES6 Template Literals . It includes multi-line strings and line interpolation.

Example:

var name = 'John',
    age = 19;

console.log(`${name} is my name. I'm ${age}.`); 

// => John is my name. I'm 19.

TypeScript 1.4 ES6 ES3/ES5.

+5

, , , #.

`${name} is my name. Greeting ${sender}`

TypeScript, , . , , .

. - TypeScript .

+2

Extention.ts

interface String {
   format: (o:Object) => string;
}

String.prototype.format = function (o) : string {
   return this.replace(/{([^{}]*)}/g,
     function (a, b) {
       var r = o[b];
       return typeof r === 'string' || typeof r === 'number' ? r : a;
     }
   );
 };

, , ,

import '../../shared/interpolation'

export class ... {

    someMethod():void {
       var str = "{name} is my name. Greeting {sender}";
       let aa  = str.format({a:'dinkar',b:'Darth Vader'});
       console.log(aa);
    }
}
0
source

Source: https://habr.com/ru/post/1667137/


All Articles