Reputation: 473
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.
Upvotes: 5
Views: 684
Reputation: 4609
You can manually compile a template. Inject the $compile
service and compile the template against a $scope
object:
$scope.model = {
name: 'tom'
};
$compile('<div>Hello {{model.name}}</div>')($scope);
This will return a jqLite object that is wrapped around a DOM object.
Upvotes: 3
Reputation: 3206
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.
Upvotes: 3