我如何能得到一个反向数组在角? 我试图使用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>

当前回答

如果您使用1.3。X,你可以使用下面的方法

{{ orderBy_expression | orderBy : expression : reverse}}

按出版日期降序列出图书

<div ng-repeat="book in books|orderBy:'publishedDate':true">

来源:https://docs.angularjs.org/api/ng/filter/orderBy

其他回答

很抱歉在一年后才提出这个问题,但有一个新的、更简单的解决方案,适用于Angular v1.3.0-rc。5及以上。

在文档中提到: “如果没有提供属性,(例如。'+'),然后数组元素本身用于比较排序"。所以,解决方案是:

ng-repeat="friend in friends | orderBy:'-'"或

ng-repeat="朋友中的朋友| orderBy:'+':true"

这种解决方案似乎更好,因为它不修改数组,也不需要额外的计算资源(至少在我们的代码中)。我已经阅读了所有现有的答案,但仍然更喜欢这个答案。

简单解决方案:-(不需要做任何方法)

ng-repeat = "friend in friends | orderBy: reverse: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

您可以通过$index参数进行反转

<tr ng-repeat="friend in friends | orderBy:'$index':true">

这是因为您使用的是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"}]