在我的Angular代码中,我遇到了以下错误。我很难理解为什么getDrawWithResults函数会引起一次digest周期,因为似乎没有任何副作用?它只是从一个设置为true的属性列表中返回项目。
只有当第一次使用getDrawWithResults时才会出现此错误,如果删除它,则错误会停止。
Uncaught Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting!
Watchers fired in the last 5 iterations: [["getDrawsWithResults(selectedLottery.draws); newVal: []; oldVal: []"],["getDrawsWithResults(selectedLottery.draws); newVal: []; oldVal: []"],["getDrawsWithResults(selectedLottery.draws); newVal: []; oldVal: []"],["getDrawsWithResults(selectedLottery.draws); newVal: []; oldVal: []"],["getDrawsWithResults(selectedLottery.draws); newVal: []; oldVal: []"]]
这是我的代码:
HTML
<h4 ng-cloak ng-hide="getDrawsWithResults(selectedLottery.draws)">Results of Selected Lottery</h4>
<div class="con" ng-repeat="draw in getDrawsWithResults(selectedLottery.draws)" ng-cloak>
<h5 class="con__header">[[ draw.date|date:'EEEE d MMMM yyyy - H:mm' ]]</h5>
<div class="balls-list__outer con__row">
<div class="balls-list">
<div class="balls-list__ball__outer" ng-repeat="b in draw.results">
<button class="balls-list__ball btn btn-con">[[ b ]]</button>
</div>
</div>
</div>
</div>
JS
// return list of draws with results
$scope.getDrawsWithResults = function(draws) {
return _.filter(draws, function(draw){
return draw.results && draw.results.length > 0;
});
}
ng-hide
也起作用了。正如预期的那样,每次绑定时都会调用withResults
过滤器2次。你能否创建一个类似的plunker来重现这个问题? - Kos ProvgetDrawsWithResults
(您的原始代码)。显然,它也可以工作,所以我假设新数组会混淆Angular的digest是不正确的。我认为问题出在其他地方。也许是selectedLotterry
或selectedLotterry.draws
在digest期间发生了变化。 - Kos Provng-repeat
在selectedLottery.draws
数组上注册了一个$scope.$watchCollection
。这意味着它监视它的元素而不是实际引用。另一方面,ng-hide
注册了一个$scope.$watch
并接受一些通过一些启发式测试(称为toBoolean
的私有函数)评估为true
的东西。每次数组引用更改时,该观察将触发,这总是导致摘要失败。我还将编辑我的答案。 - Kos Provapp.filter('withResults'
后面缺少逗号。2)代码块末尾缺少一个闭合括号。但是 S.O. 不允许仅编辑两个字符。 - James Cazzetta