yqfang
yqfang

Reputation: 37

Looking for the simplest way to make $digest throw "Maximum iteration limit exceeded" exception

From the doc for angular about $digest

,we know that the infinite loop may happen, however, I haven't encounter such an exception in my project, so please give an simplest demo to show how this exception would ocurr?

Upvotes: 1

Views: 183

Answers (1)

PSL
PSL

Reputation: 123739

It should be simple.

Use an ng-bind call a method

 <div ng-bind="someFunc()"></div>

and in the controller do:

$scope.someFunc= function(){
    return []; /*return a New instance*/
}

So ng-bind runs every digest cycle, and in this case expression of ng-bind is a function invocation which gets evaluated during the first digest cycle and a new object is returned from the function every time. It leads to a new digest cycle to happen again to stabilize DOM with the new object and ng-bind expression again gets evaluated. This(stabilization) goes on till the angular set max error limit of 10 is reached.

angular.module('app', []).controller('ctrl', function($scope) {
  $scope.callMe = function() {
    return [];
  }
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.js"></script>
<div ng-app="app" ng-controller="ctrl">
  <div ng-bind="callMe()"></div>
  Nothing Here check the console
</div>

Upvotes: 1

Related Questions