我假设_.filter
每次运行时都会返回一个新的数组实例。这会导致角度隐式$watch
就像:
ng-hide="getDrawsWithResults(selectedLottery.draws)"
and
ng-repeat="draw in getDrawsWithResults(selectedLottery.draws)"
认为模型已经改变,因此需要再次消化。
我会实施一个filter http://docs.angularjs.org/guide/filter
app.filter('withResults', function() {
return function(draws) {
return _.filter(draws, function(draw){
return draw.results && draw.results.length > 0;
});
}
})
并像这样应用它(参见下面的编辑):
ng-hide="selectedLottery.draws | withResults"
and
ng-repeat="draw in selectedLottery.draws | withresults"
在评论中讨论后编辑
实际的问题是这个绑定:
ng-hide="getDrawsWithResults(selectedLottery.draws)"
ng-hide
注册一个将永远触发的手表,因为过滤数组的引用总是会改变。可以改为:
ng-hide="getDrawsWithResults(selectedLottery.draws).length > 0"
以及相应的过滤器:
ng-hide="(selectedLottery.draws | withResults).length > 0"
ng-repeat
没有同样的问题,因为它注册了一个$watchCollection
.