我们有一个 Angular 1.6 过滤器,它一直在触发。
HTML 看起来像:
<div class="row" ng-repeat="(promptId, q) in (categoryDoubleFiltered = (categoryFiltered |
custom:searchText:selectAllCheckbox:answeredCheckbox))">
因此有 3 个参数被传递给过滤器 =>
searchText:selectAllCheckbox:answeredCheckbox
这是过滤器:
app.filter('custom', function () {
return function (input, search, selectAllCheckbox, selectAnswered) {
console.log('filter is invoked!');
// do our filtering thing
// return some subset of input
};
});
在 (promptId, q) 键/值的 HTML 中,我们有标准 ng-model
和ng-click
东西。但我不明白为什么应该调用过滤器,除非过滤器的输入之一发生变化!?当我们将鼠标悬停在 <a>
上时,甚至会调用过滤器标签。
到底是什么?我们能做些什么来阻止它被如此频繁地调用?
最佳答案
除非您使用原语,否则过滤器将在每个 $digest 中触发多次。在这种情况下,您似乎正在运行无限的摘要。您可以使用它来验证您是否确实正在运行不间断摘要:
var digestCount = 0;
$rootScope.$watch(function() {
digestCount++;
console.log(digestCount);
});
我建议您找出无限摘要的原因并修复它,以使过滤器正常工作。另外,plunkr 或 fiddle 也可以帮助我们诊断。
关于javascript - 即使过滤器参数未更改,也会调用 Angular 过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43531253/