Variable set in ng-repeat not being created

时光总嘲笑我的痴心妄想 提交于 2019-12-12 02:07:13

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!