javascript 这对于 AngularJs 过滤来说是正常的吗

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11676901/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 13:58:58  来源:igfitidea点击:

Is this normal for AngularJs filtering

javascriptangularjs

提问by John Fawcett

I'm pretty much new to angular, but I feel like this is kind of crazy.

我对 angular 很陌生,但我觉得这有点疯狂。

I've got multiple collections being displayed via ng-repeat in one controller scope. There's an input field for each list to perform a simple query. I was playing around with various filters in my code and I ended up putting a console.log in my filter function. I realized that every time my filter function was called for one list, it was being called for all of the lists in the scope. Furthermore, it was calling the filter function twice each time. So with 3 collections, filtering one of the lists would call the filter function 6 times.

我在一个控制器范围内通过 ng-repeat 显示了多个集合。每个列表都有一个用于执行简单查询的输入字段。我在我的代码中使用了各种过滤器,最终在我的过滤器函数中放置了一个 console.log。我意识到每次为一个列表调用过滤器函数时,都会为作用域中的所有列表调用它。此外,它每次都调用过滤器函数两次。因此,对于 3 个集合,过滤其中一个列表将调用过滤器函数 6 次。

I thought maybe it was just my custom filter, so I tried it out on the default filter function. Same story. Here's my code:

我想也许这只是我的自定义过滤器,所以我在默认过滤器功能上尝试了它。相同的故事。这是我的代码:

https://dl.dropbox.com/u/905197/angular-filter-test.html

https://dl.dropbox.com/u/905197/angular-filter-test.html

Go to the console and see for yourself :/

转到控制台并亲自查看:/

What am I doing wrong here? This seems like such a simple thing but it's doing so much work.

我在这里做错了什么?这似乎是一件很简单的事情,但它做了很多工作。

回答by Renan Tomal Fernandes

This is normal, angularjs uses a 'dirty-check' approach, so it needs to call all the filters to see if any changes exist. After this it detects that you have a change on one variable (the one that you typed) and then it re-executes all filters again to detect if it has other changes.

这是正常的,angularjs 使用“脏检查”方法,因此它需要调用所有过滤器来查看是否存在任何更改。在此之后,它检测到您对一个变量(您键入的那个)进行了更改,然后再次重新执行所有过滤器以检测它是否有其他更改。

See the first answer of thisquestion

看到这个问题的第一个答案