mirror of
https://github.com/zhigang1992/angular.js.git
synced 2026-04-03 17:15:37 +08:00
Mention common cause of error is binding to a new array on every $digest loop. Closes #6465
42 lines
1.3 KiB
Plaintext
42 lines
1.3 KiB
Plaintext
@ngdoc error
|
|
@name $rootScope:infdig
|
|
@fullName Infinite $digest Loop
|
|
@description
|
|
|
|
This error occurs when the application's model becomes unstable and each `$digest` cycle triggers a state change and subsequent `$digest` cycle.
|
|
Angular detects this situation and prevents an infinite loop from causing the browser to become unresponsive.
|
|
|
|
For example, the situation can occur by setting up a watch on a path and subsequently updating the same path when the value changes.
|
|
|
|
```
|
|
$scope.$watch('foo', function() {
|
|
$scope.foo = $scope.foo + 1;
|
|
});
|
|
```
|
|
|
|
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}.
|