javascript - Angular ngRepeat orderBy order in Object -


i have following json data , i'd angular ngrepeat each key inside specification , displayed in same order in object:

"specification": {     "screen": "15.4\" retina display",     "resolution": "2880 x 1800",     "hdd": "512gb ssd",     "ram": "16gb ram",     "cpu": "intel core i7 2.30 ghz processor",     "software": "mac os x 10.9 mavericks" } 

this html:

<ul>     <li ng-repeat="(key, value) in selectedproduct.specification">         <span ng-class="{{key | lowercase}}">{{value}}</span>     </li> </ul> 

the above code outputs data in random order, not order of appearance in array.

some browsers (e.g. chrome) re-arrange keys alphabetically, there no reliable, cross-browser way retain key order object. should either turn array, or specify key order separately (e.g. separate array).


update

as runturm correctly pointed out, in ngrepeat keys ordered alphabetically angular. so, easiest way retain specific order use separate array holding keys in desired order:

/* controller: */ $scope.keys = ["screen", "resolution", "hdd", "ram", "cpu", "software"]; $scope.selectedproduct = {     specification: {         ...     },     ... };  <!-- view: --> <ul>     <li ng-repeat="key in keys">         <span class="{{key | lowercase}}">{{selectedproduct.specification[key]}}</span>     </li> </ul> 

if find more readable or if plan use selectedproduct.specification[key] in multiple places, use nginit alias value:

    <li ng-repeat="key in keys" ng-init="value=selectedproduct.specification[key]">         <span class="{{key | lowercase}}">{{value}}</span>     </li> 

see, also, short demo.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -