Angularjs:为什么ng-repeat orderBy:数据属性更改时重新排序错误

时间:2014-01-22 04:50:42

标签: angularjs angularjs-directive angularjs-ng-repeat

我使用orderBy通过is_important属性&来对ng-repeat中的数据进行排序。 desc,如下所示:

<li ng-repeat="data in datas | orderBy:'-is_important'">{{data.id}}</li>

$scope.datas =  [{"id":"1","is_important":"0"},
                 {"id":"2","is_important":"0"},
                 {"id":"3","is_important":"0"},
                 {"id":"4","is_important":"1"},
                 {"id":"5","is_important":"0"},
                 {"id":"6","is_important":"0"}];

开始时,屏幕上显示的订单是正确的

4
1
2
3
5
6

但是当我改变is_important属性

$scope.datas[3].is_important = 0;
$scope.datas[5].is_important = 1;

ng-repeat显示的结果:

1
2
3
5
6
4

与我预期的答案不一样:

6
1
2
3
4
5

为什么呢?

这是我的代码: http://jsbin.com/oMUSeHO/1/edit

1 个答案:

答案 0 :(得分:4)

因为在change方法中,您将is_important设置为数字。

$scope.datas[3].is_important = 0;
$scope.datas[5].is_important = 1;

按如下方式更改

$scope.datas[3].is_important = "0";
$scope.datas[5].is_important = "1";