AngularJS filter triggers infinite $digest loop

前端 未结 2 1318
星月不相逢
星月不相逢 2021-01-19 01:05

I want to use a filter expression like values | filter:value1 to hide or show a div, where values and value are both declared on the s

2条回答
  •  天涯浪人
    2021-01-19 02:04

    May be because of using filter inside ng-show, filter gets executed in every digest cycle, and keeps checking your value is changed or not, if value is changed then it evaluates again and gives new result, otherwise it doesn't do anything (becaues angularJs uses "dirty-checking" method to find any change)

    http://jsbin.com/nujineci/4/edit

    check this link, i have modified

    ng-show="g.values | filter:g.value1"
    

    To

    ng-show="g.values.indexOf(g.value1) != -1"
    

    or ng-show="g.values.indexOf(g.value1) > -1" // this is same as above

    indexOf returns index of item if exists in array, otherwise returns -1

    I use this type of syntax, never had any problem with this

提交回复
热议问题