角度 ng-repeat 反向 [英] angular ng-repeat in reverse

查看:45
本文介绍了角度 ng-repeat 反向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能得到一个有角度的反转数组?我正在尝试使用 orderBy 过滤器,但它需要一个谓词(例如名称")来排序:

How can i get a reversed array in angular? i'm trying to use orderBy filter, but it needs a predicate(e.g. 'name') to sort:

<tr ng-repeat="friend in friends | orderBy:'name':true">
      <td>{{friend.name}}</td>
      <td>{{friend.phone}}</td>
      <td>{{friend.age}}</td>
<tr>

有没有办法反转原始数组,而不需要排序.像这样:

Is there a way to reverse original array, without sorting. like that:

<tr ng-repeat="friend in friends | orderBy:'':true">
      <td>{{friend.name}}</td>
      <td>{{friend.phone}}</td>
      <td>{{friend.age}}</td>
<tr>

推荐答案

我建议使用自定义过滤器,例如:

I would suggest using a custom filter such as this:

app.filter('reverse', function() {
  return function(items) {
    return items.slice().reverse();
  };
});

然后可以像这样使用:

<div ng-repeat="friend in friends | reverse">{{friend.name}}</div>

在这里查看它的工作原理:Plunker 演示

See it working here: Plunker Demonstration

此过滤器可以根据您的需要进行定制,以满足您的需求.我在演示中提供了其他示例.一些选项包括在执行反转之前检查变量是否为数组,或者使其更宽松以允许反转更多事物,例如字符串.

This filter can be customized to fit your needs as seen fit. I have provided other examples in the demonstration. Some options include checking that the variable is an array before performing the reverse, or making it more lenient to allow the reversal of more things such as strings.

这篇关于角度 ng-repeat 反向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆