如何解决ng-repeat AngularJS中的方法sort() ?

时间:2021-08-04 07:43:23

I created an array with numbers, but in the method sort() not ordering it correctly in ng-repeat.

我创建了一个带有数字的数组,但是在方法sort()中,在ng-repeat中没有正确排序。

eg

$scope.order = ['1', '10', '11', '22', '29', '2'];

eg

<li ng-repeat="key in order.sort()">{{key}}</li>

see how ordered

看看命令

1 10 11 2 22 29

not

1 2 10 11 22 29

eg http://jsfiddle.net/YWsJ7/

比如http://jsfiddle.net/YWsJ7/

2 个解决方案

#1


2  

You can use orderBy filter with expression parameter. In the following example each value of the array will be passed to parseInt function and then values will be compared using <, > and =:

您可以使用带有表达式参数的orderBy过滤器。在下面的示例中,数组的每个值将传递给parseInt函数,然后将使用<、>和=:

JavaScript

JavaScript

function mainCtrl($scope) {  
    $scope.parseInt = parseInt;
    $scope.order = ['1', '10', '11', '22', '29', '2'];
}

HTML

HTML

<li ng-repeat="key in order | orderBy : parseInt ">{{key}}</li>

Fiddle: http://jsfiddle.net/5kRRc/3/

小提琴:http://jsfiddle.net/5kRRc/3/

Documentation on orderBy filter

文档orderBy过滤器

#2


1  

You can make use of the angular filter method:

可以利用角滤法:

 <li ng-repeat="key in order | orderBy: 'value'">{{key.value}}</li>

JSFIDDLE

JSFIDDLE

#1


2  

You can use orderBy filter with expression parameter. In the following example each value of the array will be passed to parseInt function and then values will be compared using <, > and =:

您可以使用带有表达式参数的orderBy过滤器。在下面的示例中,数组的每个值将传递给parseInt函数,然后将使用<、>和=:

JavaScript

JavaScript

function mainCtrl($scope) {  
    $scope.parseInt = parseInt;
    $scope.order = ['1', '10', '11', '22', '29', '2'];
}

HTML

HTML

<li ng-repeat="key in order | orderBy : parseInt ">{{key}}</li>

Fiddle: http://jsfiddle.net/5kRRc/3/

小提琴:http://jsfiddle.net/5kRRc/3/

Documentation on orderBy filter

文档orderBy过滤器

#2


1  

You can make use of the angular filter method:

可以利用角滤法:

 <li ng-repeat="key in order | orderBy: 'value'">{{key.value}}</li>

JSFIDDLE

JSFIDDLE