Remove Auto Sort with ng-repeat

I have a json object. It is not ordered in ascending order.

$scope.myData = { "MAX" : "some value", "Forms" : "some value", "Grids And Tables" : "some value", "Navigation" : "some value", "Services & APIs" : "some value" } 

I used ng-repeat to show in my html template. I got the result, but the order has changed in ascending order.

 Forms Grids And Tables MAX Navigation Services & APIs 

How to prevent angular js from automatically ordering?

Check link

+7
angularjs order ng-repeat
source share
3 answers

As suggested, it is not possible to sort keys of objects. Buy can be sorted by an array created using these object keys -

 $scope.keys = Object.keys($scope.myData); <ul> <li ng-repeat="key in keys"> {{key}} - {{myData[key]}} </li> </ul> 
+9
source share

The iteration order over object keys in javascript is technically undefined. Although most browsers will iterate over an object in a specific order, you should not rely on this behavior.

In the case of angular, ng-repeat you must always sort the keys so that the order is determinate even when the keys are added after the fact. It is not possible to change this behavior because it violates the directive because the keys will be added or deleted dynamically.

If you need to rely on ordering your data, the hash of the object is not the correct data structure. You must either reformat your data as an array, or simply create a separate array with the order of the keys and perform ng-repeat on it. Using keys to index into an object.

+2
source share

You must name your keys manually as 1,2,3,4,5, and then cut them at the front end with string.slice (1). Hope this helps.

0
source share

All Articles