0

I have an angular web application, so I would like to use angular also for string interpolations. For example, I would like to make some replacements in myTemplate:

var myTemplate = "Make something awesome with a = '{{scope.a}}' and b = '{{scope.b}}'.";
var scope = {
  a: 1,
  b: 2
};

I know how to do this with underscore.js:

_.template(myTemplate)(scope);

I'm wondering if it is possible in angular?

0

2 Answers 2

3

Use $interpolate service:

var myTemplate = "Make something awesome with a = '{{a}}' and b = '{{b}}'.";
var scope = {
    a: 1,
    b: 2
};

$interpolate(myTemplate)(scope); // "Make something awesome with a = '1' and b = '2'."

Only with above scope object structure, you should use {{a}} instead of {{scope.a}}.

Demo: http://plnkr.co/edit/aoaeWekhkLP5k7NR2RWB?p=preview

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

Comments

1

Yes you can make use of angular's $interpolate service

var myTemplate = "Make something awesome with a = '{{a}}' and b = '{{b}}'.";
var fn = $interpolate(myTemplate);

var scope = {
  a: 1,
  b: 2
};


// Make something awesome with a = '1' and b = '2'.
fn(scope);

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.