简体   繁体   中英

Angular -> ng-repeat filtered array variable

Below I will put link to my example in Plunker to show you my problem. 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. 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?

PS. run your web browser console and you will see the difference.

example Plunker

Use ng-blur instead of 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.

When you change value, first ng-change is called, then $digest starts which will update filtrowane . If you use such notations:

item in filtrowane = (tablica | filter:search)

do not use filtrowane in controller. Html should be view, so you should not declare any variables there, which you use in model (js).

Look here: http://plnkr.co/edit/QAHlbuZqWX4szglvMP87?p=preview This code does same as yours, but filtering is done in javascript. A bit more complicated, but things are more clear. (it is also better for big arrays)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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