寻找使 $digest 抛出 "Maximum iteration limit exceeded" 异常的最简单方法
Looking for the simplest way to make $digest throw "Maximum iteration limit exceeded" exception
来自 angular 关于 $digest
的文档
,我们知道可能会出现死循环,但是,我在项目中没有遇到过这样的异常,所以请给一个最简单的demo来说明这个异常是如何发生的?
应该很简单。
使用 ng-bind 调用方法
<div ng-bind="someFunc()"></div>
并在控制器中执行:
$scope.someFunc= function(){
return []; /*return a New instance*/
}
所以 ng-bind
在每个摘要周期运行,在这种情况下,ng-bind 的表达式是一个函数调用,它在第一个摘要周期中被评估,并且每次从函数返回一个新对象。它导致新的摘要循环再次发生以稳定 DOM 新对象,并且 ng-bind 表达式再次被评估。这种(稳定)一直持续到 angular 设置的最大错误限制为 10。
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>
来自 angular 关于 $digest
的文档,我们知道可能会出现死循环,但是,我在项目中没有遇到过这样的异常,所以请给一个最简单的demo来说明这个异常是如何发生的?
应该很简单。
使用 ng-bind 调用方法
<div ng-bind="someFunc()"></div>
并在控制器中执行:
$scope.someFunc= function(){
return []; /*return a New instance*/
}
所以 ng-bind
在每个摘要周期运行,在这种情况下,ng-bind 的表达式是一个函数调用,它在第一个摘要周期中被评估,并且每次从函数返回一个新对象。它导致新的摘要循环再次发生以稳定 DOM 新对象,并且 ng-bind 表达式再次被评估。这种(稳定)一直持续到 angular 设置的最大错误限制为 10。
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>