Nat Nat - 2 months ago 6x
AngularJS Question

AngularJS filter triggers infinite $digest loop

I want to use a filter expression like

values | filter:value1
to hide or show a div, where
are both declared on the scope in the containing directive's link function.

This works as expected, except that it keeps firing off infinite digest loop errors.

See this jsbin for a demonstration:

What am I doing wrong? Why should these errors occur when no models are being updated by the filter?


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)

check this link, i have modified

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


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