角NG重复反向 [英] angular ng-repeat in reverse

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

问题描述

我怎样才能在一个角阵逆转?
我试图使用过滤器排序依据,但它需要一个predicate排序(例如名称。):

 < TR NG重复=朋友的朋友|排序依据:'名':真正的>
      &所述; TD> {{friend.name}}&下; / TD>
      &所述; TD> {{friend.phone}}&下; / TD>
      &所述; TD> {{friend.age}}&下; / TD>
&所述; TR>

有没有办法扭转原来的阵列,不排序。
这样的:

 < TR NG重复=朋友的朋友|排序依据:'':真正的>
      &所述; TD> {{friend.name}}&下; / TD>
      &所述; TD> {{friend.phone}}&下; / TD>
      &所述; TD> {{friend.age}}&下; / TD>
&所述; TR>


解决方案

我会建议使用自定义过滤器,如这样的:

app.filter(反向,函数(){
  返回功能(项){
    返回items.slice()反向()。
  };
});

然后可以使用这样的:

< D​​IV NG重复=朋友的朋友|反向> {{friend.name}}< / DIV>

看到它在这里工作: Plunker示范


此过滤器可定制为挺直腰杆,以满足您的需求。我已经在示范提供其它实例。某些选项包括检查该变量进行反向,或使其更加宽松,让更多的事情逆转,如之前的字符串数组。

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();
  };
});

Which can then be used like:

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

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重复反向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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