In AngularJS, how can I render a value without 2-way data binding? One may want to do this for performance reasons, or even rendering a value at a given point in time.
The following examples both use data binding:
<div>{{value}}</div>
<div data-ng-bind="value"></div>
How do I render value
without any data binding?
Angular 1.3+
In 1.3, Angular has supported this using the following syntax.
<div>{{::message}}</div>
As mentioned in this answer.
Angular 1.2 and below
This is simple and doesn't need a plugin. Check this out.
This small directive will easily accomplish what you are trying to achieve
app.directive('bindOnce', function() {
return {
scope: true,
link: function( $scope ) {
setTimeout(function() {
$scope.$destroy();
}, 0);
}
}
});
You can bind once like this
<div bind-once>I bind once - {{message}}</div>
You can bind like normal
<div ng-bind="message" bind-once></div>
Demo: http://jsfiddle.net/fffnb/
Some of you may be using angular batarang, and as mentioned in the comments if you use this directive the element still shows as binding when it is not, I am pretty sure this has something to do with the classes that are attached to the element so try this, it should work (not tested). Let me know in the comments if it worked for you.
app.directive('bindOnce', function() {
return {
scope: true,
link: function( $scope, $element ) {
setTimeout(function() {
$scope.$destroy();
$element.removeClass('ng-binding ng-scope');
}, 0);
}
}
});
@x0b: If you have OCD and you want to remove the empty class
attribute do this
!$element.attr('class') && $element.removeAttr('class')