0

Following this question on how to determine number of filtered out elements, I created the following angular code:

<input type="text" ng-model="query" placeholder="Enter query...">
<div ng-if="filteredData.length!=data.length"> Showing {{filteredData.length}} of {{data.length}}</div>
<div ng-repeat="datum in filteredData = (data | orderBy: 'name' : reverse | filter:query)"> 
    ....
</div>

However, for some reason filteredData never actually gets set and Showing of 10 always appears. I used this answer to get the scope variable for my controller, but filteredData doesn't show up there despite all the other variables showing up.

Also worth noting: the contents of the repeat work fine, its just that filteredData that isnt being set.

Community
  • 1
  • 1
David says Reinstate Monica
  • 19,209
  • 22
  • 79
  • 122
  • please see here http://jsbin.com/xuyuy/3/edit I've used your code and everything seems fine. have you any error on console ? – sylwester Oct 13 '14 at 15:00
  • @sss I figured out the issue. Its because my `ng-repeat` was wrapped in an `ng-if`, while the rest of the code wasn't. However, the `ng-if` was always evaluating to true, so I don't know why the `ng-if` was causing issues. – David says Reinstate Monica Oct 13 '14 at 15:40

1 Answers1

0

I believe this is because you are trying to set filteredData within your ng-repeat.

You should probably be setting filteredData to be a filtered set of your data in the controller. That way you are just looping in your repeat statement.

<div ng-repeat="datum in filteredData | orderBy: 'name' : reverse | filter:query"> 
    ....
</div>

It's also probably worth noting that angular filter in ng-repeat wont edit your array's length. So in the case that your code actually DID work the filteredData.length wouldn't change.

DevVex
  • 143
  • 1
  • 6
  • The angular filter is not meant to change the arrays length, its meant to create a subset and assign it to a new array,`filteredData`, which will have a smaller length. This used to work, but now it stopped working for some reason. Doing it inside out outside of the controller shouldn't matter. – David says Reinstate Monica Oct 13 '14 at 14:21