我如何能得到一个反向数组在角?
我试图使用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>
这是我使用的:
<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"}]