我如何能得到一个反向数组在角?
我试图使用orderBy过滤器,但它需要一个谓词(例如。'name')排序:
<tr ng-repeat="friend in friends | orderBy:'name':true">
<td>{{friend.name}}</td>
<td>{{friend.phone}}</td>
<td>{{friend.age}}</td>
<tr>
是否有一种方法可以反转原始数组,而不需要排序。
像这样:
<tr ng-repeat="friend in friends | orderBy:'':true">
<td>{{friend.name}}</td>
<td>{{friend.phone}}</td>
<td>{{friend.age}}</td>
<tr>
很抱歉在一年后才提出这个问题,但有一个新的、更简单的解决方案,适用于Angular v1.3.0-rc。5及以上。
在文档中提到:
“如果没有提供属性,(例如。'+'),然后数组元素本身用于比较排序"。所以,解决方案是:
ng-repeat="friend in friends | orderBy:'-'"或
ng-repeat="朋友中的朋友| orderBy:'+':true"
这种解决方案似乎更好,因为它不修改数组,也不需要额外的计算资源(至少在我们的代码中)。我已经阅读了所有现有的答案,但仍然更喜欢这个答案。
这是我使用的:
<alert ng-repeat="alert in alerts.slice().reverse()" type="alert.type" close="alerts.splice(index, 1)">{{$index + 1}}: {{alert.msg}}</alert>
更新:
对于老版本的Angular,我的答案是OK的。
现在,你应该使用
ng-repeat="friend in friends | orderBy:'-'"
or
ng-repeat="friend in friends | orderBy:'+':true"
从https://stackoverflow.com/a/26635708/1782470
这是因为您使用的是JSON对象。当你面对这样的问题时,把JSON对象改为JSON数组对象。
例如,
{"India":"IN","America":"US","United Kingdon":"UK"} json object
[{"country":"India","countryId":"IN"},{"country":"America","countryId":"US"},{"country":"United Kingdon","countryId":"UK"}]