dyang dyang - 1 year ago 49
AngularJS Question

Angular evaluate expression as if it were in the HTML

I am looking for a function that will do something like this in Angular:

var testValues = {
name: 'John Doe',
number: 15
somefunction('Hello, {{name}}, you are {{number}} years old', testValues)
// returns 'Hello, John Doe, you are 15 years old'

I know $eval does something similar to this, but it cannot have double braces inside of it.

Answer Source

You are probably looking for the $interpolate service.

Example from the documentation:

var $interpolate = ...; // injected
var exp = $interpolate('Hello {{name | uppercase}}!');
expect(exp({name:'Angular'})).toEqual('Hello ANGULAR!');

$interpolate compiles a string just like in the templates, and returns a function that outputs a string using the provided context.