AngularJS过滤器服务循环 [英] AngularJS filter service on loop
问题描述
我一直在尝试为表数据创建自定义过滤器,但是这样做时,在登录到控制台时,在无限循环中调用了过滤器功能/
I've been trying to create a custom filter for table data, but when doing so, when logging to the console the filter function was being called on an infinite loop/
然后,我决定使用单独的数据运行测试,并在同一页面上使用简单的过滤器查看问题是否仍然存在,并从此处复制
I then decided to run a test with separate data, with a simple filter on the same page to see if the issue persisted and it does, copied from here How to filter multiple values (OR operation) in angularJS.
<div ng-init="movies = [
{title:'Man on the Moon', genre:'action'},
{title:'Meet the Robinsons', genre:'family'},
{title:'Sphere', genre:'action'}
];" >
<input type="checkbox" ng-model="genrefilters.action" />Action
<br />
<input type="checkbox" ng-model="genrefilters.family" />Family
<br />{{genrefilters.action}}::{{genrefilters.family}}
<ul>
<li ng-repeat="movie in movies | bygenre:genrefilters">{{movie.title}}: {{movie.genre}}</li>
</ul>
</div>
angular.module('ppApp').
filter('bygenre', function () {
return function (movies, genres) {
console.log('hello')
var items = {
genres: genres,
out: []
};
angular.forEach(movies, function (value, key) {
if (this.genres[value.genre] === true) {
this.out.push(value);
}
}, items);
return items.out;
};
});
过滤器仍在我的应用程序中循环.我在plnkr中测试了提供的代码,效果很好.
The filter is still on loop in my application. I tested the provided code in a plnkr and it worked fine.
更新
因此在控制台中调试脚本时,正在使用component
服务来更新时间.与用于过滤表的控制器完全分开.在我使用的$timeout
函数上,它似乎在每次请求新时间时都调用digest
循环.
So debugging the script in console a component
service was being used to update the time. Completely separate from the controller I am using for filtering the table. On the $timeout
function I was using it seemed to be calling the digest
cycle every time it made the request for the new time.
问题
为什么在调用$timeout
服务但没有其他控制器或其他服务时调用过滤器服务?
Why is the filter service being called when a $timeout
service is called, but no other controllers or other services?
推荐答案
AngularJS框架在每个摘要周期评估AngularJS表达movies | bygenre:genrefilters
,以确定是否有任何变化.
The AngularJS framework evaluates the AngularJS expresssion movies | bygenre:genrefilters
every digest cycle to determine any changes.
脏检查属性更改的范围是AngularJS中的常见操作.它在与AngularJS框架集成的每个浏览器事件之后发生.
Dirty checking the scope for property changes is a common operation in AngularJS. It happens after every browser event that is integrated with the AngularJS framework.
有关更多信息,请参见
- AngularJS Developer Guide - Integration with the Browser Event Loop
- How does data binding work in AngularJS?
这篇关于AngularJS过滤器服务循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!