Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angularjs: Why ng-repeat orderBy: re-sort incorrectly when data property change

I use orderBy to sort my data in ng-repeat by is_important property & desc, like below:

<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"}];

At the beginning, the order show on the screen is correct

4
1
2
3
5
6

But when I change the is_important property

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

the result displayed by ng-repeat :

1
2
3
5
6
4

isn't same as my expected answer:

6
1
2
3
4
5

Why?

this is my code: http://jsbin.com/oMUSeHO/1/edit

like image 601
Chen-Tsu Lin Avatar asked Apr 30 '26 21:04

Chen-Tsu Lin


1 Answers

Because in change method you set is_important to digit.

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

Change it as follows

$scope.datas[3].is_important = "0";
$scope.datas[5].is_important = "1";
like image 102
Alborz Avatar answered May 02 '26 11:05

Alborz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!