简体   繁体   English

Angular-> ng-repeat过滤数组变量

[英]Angular -> ng-repeat filtered array variable

Below I will put link to my example in Plunker to show you my problem. 下面,我将链接到我在Plunker中的示例,以向您展示我的问题。 In ng-repeat I have used array(filtrowane) to store data from filter result, also I have added ng-change on input for showing the value of filtrowane.length in console. ng-repeat我使用array(filtrowane)来存储来自过滤器结果的数据,我还在输入上添加了ng-change,以在控制台中显示filtrowane.length的值。 Variable filtrowane is defined in controller, and after typing something It`s showing different values in console and in the view, can you tell me why? 变量filtrowane在控制器中定义,键入一​​些内容后,它在控制台和视图中显示不同的值,您能告诉我为什么吗?

PS. PS。 run your web browser console and you will see the difference. 运行您的Web浏览器控制台,您将看到区别。

example Plunker 样例

Use ng-blur instead of ng-change 使用ng-blur而不是ng-change

<input type="text" ng-model="search" ng-blur="sprawdzFiltr()">

This is due to the ng-change is probably executed before the filter is applied, so the old value will be taken. 这是由于ng-change可能是在应用过滤器之前执行的,因此将采用旧值。

When you change value, first ng-change is called, then $digest starts which will update filtrowane . 更改值时,首先调用ng-change,然后$ digest启动,这将更新filtrowane If you use such notations: 如果使用这样的符号:

item in filtrowane = (tablica | filter:search)

do not use filtrowane in controller. 不要在控制器中使用filtrowane Html should be view, so you should not declare any variables there, which you use in model (js). HTML应该是view,因此您不应在其中声明在模型(js)中使用的任何变量。

Look here: http://plnkr.co/edit/QAHlbuZqWX4szglvMP87?p=preview This code does same as yours, but filtering is done in javascript. 在这里查看: http : //plnkr.co/edit/QAHlbuZqWX4szglvMP87?p=preview此代码与您的代码相同,但是过滤是在javascript中完成的。 A bit more complicated, but things are more clear. 稍微复杂一点,但是事情更清楚了。 (it is also better for big arrays) (对于大型数组也更好)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM