docs(errors/infdig): add a common example

Mention common cause of error is binding to a new array on every $digest loop.

Closes #6465
This commit is contained in:
Brad Williams
2014-02-26 14:27:17 -08:00
committed by Igor Minar
parent 713f9758e2
commit fd09586b08

View File

@@ -14,4 +14,28 @@ $scope.$watch('foo', function() {
});
```
One common mistake is binding to a function which generates a new array every time it is called. For example:
```
<div ng-repeat="user in getUsers()">{{ user.name }}</div>
...
$scope.getUsers = function() {
return [ { name: 'Hank' }, { name: 'Francisco' } ];
};
```
Since `getUsers()` returns a new array, Angular determines that the model is different on each `$digest`
cycle, resulting in the error. The solution is to return the same array object if the elements have
not changed:
```
var users = [ { name: 'Hank' }, { name: 'Francisco' } ];
$scope.getUsers = function() {
return users;
});
```
The maximum number of allowed iterations of the `$digest` cycle is controlled via TTL setting which can be configured via {@link ng.$rootScopeProvider $rootScopeProvider}.