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.
AngularJS expressions can be written inside double braces: {{ expression }} . AngularJS expressions can also be written inside a directive: ng-bind="expression" . AngularJS will resolve the expression, and return the result exactly where the expression is written.
It means it's used to achieve one time binding. Example. angular.
The AngularJS expressions are expressed as {{ }} that associates data to HTML. It displays the data written inside the braces. The expressions are the JavaScript code snippets located inside the bindings.
You can use data-ng-, instead of ng-, if you want to make your page HTML valid. You will learn a lot more about directives later in this tutorial.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With