4

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

is there some module in angualar 2.0, so that i can do something like string.format() as we do in C#?

I know this can be done using vanila js custom method, but i want to know is there any module inside angular 2 to handle this. They use interpolation in template binding so how to do that with a normal string

3
  • Not sure what you are asking but if you want to override the interpolation syntax this might be helpful: stackoverflow.com/questions/39819407/… Commented Jan 18, 2017 at 12:57
  • 1
    There is developer.mozilla.org/en/docs/Web/JavaScript/Reference/…. Angular itself doesn't provide anything. Commented Jan 18, 2017 at 12:58
  • I want to interpolate the string {name} is my name. Greeting {sender} to something like "Dinkar is my mane. Greeting codef0rmer". using something like string.formate("{name} is my name. Greeting {sender}","Dinkar","codef0rmer") Commented Jan 18, 2017 at 12:59

3 Answers 3

5

Check ES6 Template literals. It enables multi-line strings and string interpolation.

Example:

var name = 'John',
    age = 19;

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

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

TypeScript from version 1.4 supports ES6 Template literals and can compile them down to ES3/ES5 expressions.

Sign up to request clarification or add additional context in comments.

2 Comments

i was looking something provided by the framework. Ends up creating prototype function for String and using it. Wonder this basic thing is not provided by the framework :(
If you're using Angular with TypeScript, then it's already available for use, since TypeScript from version 1.4 supports ES6 Template literals.
2

By using the back tick, grave, character you can achieve string interpolation similar to c#.

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

This is a feature of TypeScript but does not do any formatting such as specifying decimal places. I would recommend keeping your string definitions on a single line because I have seen multiline definitions mess up styles in certain editors.

Here is a great article about template strings. The rest of the guide is a great resource for TypeScript in general.

1 Comment

this is not a typescript feature. you this is ES6 feature.
0

I came up creating and prototype method for string class Created Extention.ts file with code

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;
     }
   );
 };

then where i wanted to use the formatting i imported the extension file and use format method like

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);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.