这对于 AngularJs 过滤来说是正常的吗 [英] Is this normal for AngularJs filtering

查看:27
本文介绍了这对于 AngularJs 过滤来说是正常的吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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.

我以为这可能只是我的自定义过滤器,所以我在默认过滤器功能上进行了尝试.相同的故事.这是我的代码:

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/角度过滤器测试.html

去控制台亲自看看:/

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

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

推荐答案

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

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 过滤来说是正常的吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆