假设你有一个非常大的导航,大约有 15 个嵌套级别,比如:
var app = angular.module("myApp", []);
app.controller("HomeCtrl", ["$scope", function($scope) {
this.entries = [{
"title": "test",
"entries": [{
"title": "sub-test",
"entries": [{
"title": "sub-test",
"entries": [{
"title": "sub-test",
"entries": [{
"title": "sub-test",
"entries": [{
"title": "sub-test",
"entries": [{
"title": "sub-test",
"entries": [{
"title": "sub-test",
"entries": [{
"title": "sub-test",
"entries": [{
"title": "sub-test",
"entries": [{
"title": "sub-test",
"entries": [{
"title": "sub-test",
"entries": [{
"title": "sub-test"
}]
}]
}]
}]
}]
}]
}]
}]
}]
}]
}]
}]
}];
return this;
}]);
您正在用ng-repeat
迭代这些:
<script type="text/ng-template" id="entryTree">
<span>{{ entry.title }}</span>
<ol ng-if="entry.entries">
<li ng-repeat="entry in entry.entries" ng-include="'entryTree'"></li>
</ol>
</script>
<div ng-controller="HomeCtrl as ctrl">
<ol>
<li ng-repeat="entry in ctrl.entries" ng-include="'entryTree'"></li>
</ol>
</div>
演示
然后抛出一个$rootScope:infdig
错误:10 $digest() iterations reached. Aborting!
。我知道这里有几个类似的问题(例如这个)。但就我而言,我没有从控制器返回动态值,也没有使用动态 getter,我只是将值静态保存在控制器中。
这是什么原因?解决方案会是什么样子?
由于 ng-include 的原因,模板的每次迭代都会导致新的摘要循环。当一个摘要循环导致另一个,并以这种方式循环 10 次时,angular 假设它可能是一个无限循环并抛出错误。