Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
149 views
in Technique[技术] by (71.8m points)

javascript - 反向角度重复(angular ng-repeat in reverse)

How can i get a reversed array in angular?

(我怎样才能得到一个角度反向阵列?)

i'm trying to use orderBy filter, but it needs a predicate(eg 'name') to sort:

(我正在尝试使用orderBy过滤器,但是它需要一个谓词(例如'name')进行排序:)

<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>
  ask by Delremm translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

(一些选项包括在执行反向操作之前检查变量是否为数组,或者使它更宽松以允许反向执行更多操作,例如字符串。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...