问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/26341910/variable-set-in-ng-repeat-not-being-created