Angular ng-repeat 中的 orderBy 数组项值 [英] orderBy array item value in Angular ng-repeat
问题描述
如何让 Angular 的 ng-repeat
指令按照每个项目的实际值而不是每个项目的属性值对列表进行排序?
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?
推荐答案
自 1.3.0-rc.5
由于 AngularJS 1.3.0-rc.5,orderBy
如果未提供其他参数,过滤器(请参阅文档)将使用其项自动对数组进行排序.
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>
orderBy
过滤器(参见 历史文档) 也可以将函数作为第二个参数,其返回值将使用 <
、=
和 >代码> 运算符.为此,您可以简单地使用
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>
这篇关于Angular ng-repeat 中的 orderBy 数组项值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!