why ng-bind is better than {{}} in angular?
<div> Hello, {{user.name}}</span></div>
<div> Hello, <span ng-bind="user.name"></span></div>
if you write like below then , you will find person is undefined(in older version of Angular).
<li ng-repeat="person in people">
Hello, {{calculateTotalPoints(person)}}
</li>
You have to write:
<li ng-repeat="person in people">
Hello, <span ng-bind="calculateTotalPoints(person)"></span>
</li>
In the controller, you can define the function:
$scope.calcualteTotalPoints = function(person){
....
};
- The {{}} is much slower.
- you might see the actual Hello, {{user.name}} for a second before user.name is resolved (before the data is loaded)
- in case of {{}}whole text "Hello, {{user.name}}" enclosed by < div > will be copied and stored in memory . but ng-bind will only stored the value of variable in memory , angular will register a watcher where the watch expression consists of the variable only.
- The brackets on the other hand will be dirty checked and refreshed in every $digest, even if it's not necessary.
- This ng-bind is a directive and will place a watcher on the passed variable. So the ng-bind will only apply, when the passed value does actually change.
<div> Hello, {{user.name}}</span></div>
<div> Hello, <span ng-bind="user.name"></span></div>
0 comments: