在角NG重复排序依据数组项值 [英] orderBy array item value in Angular ng-repeat
问题描述
我怎样才能获得角的 NG-重复
指令排序每个项目的实际值的列表,而不是在每个项目上的属性值呢?
How can I get Angular's ng-repeat
directive to sort a list by each item's actual value, rather than by the value of a property on each item?
例如:
<ul>
<li ng-repeat="item in items | orderBy:'WHAT_GOES_HERE??'">{{item}}</li>
</ul>
下面是一个小提琴玩: http://jsbin.com/okatur/1/edit
Here's a fiddle to play with: http://jsbin.com/okatur/1/edit
我知道我可以只是做的.sort()
阵列上,但是是我唯一的选择?
I realize I could just do .sort()
on the array, but is that my only option?
推荐答案
由于<一个href=\"https://github.com/angular/angular.js/commit/607f016a0ba705ce40df0164360fb96a9d7f5912\">AngularJS 1.3.0-rc.5 ,在排序依据
过滤器(请参阅文档)将自动排序。
Since 1.3.0-rc.5
Since AngularJS 1.3.0-rc.5, the orderBy
filter (see the documentation) will automatically sort the array using its items if no additional parameters are provided.
<li ng-repeat="item in items | orderBy">{{item}}</li>
<大骨节病> JS斌 骨节病>
的排序依据
过滤器(请参阅历史文献)也可以采取一个函数作为第二个参数,它的返回值将使用被比较的&LT;
, =
和&GT;
运营商。
你可以简单地使用 angular.identity
(请参见文档),用于这一目的:
The orderBy
filter (see the historical documentation) can also take a function as second parameter, whose return value will be compared using the <
, =
and >
operator.
You can simply use the angular.identity
(see the documentation) for that purpose:
$scope.identity = angular.identity;
<li ng-repeat="item in items | orderBy:identity">{{item}}</li>
<大骨节病> JS斌 骨节病>
这篇关于在角NG重复排序依据数组项值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!