Angularjs orderBy in ng-repeat not working as expected -


i have following div in template:

<div ng-repeat="item in billing_history | orderby:'-timestamp'">{{ item.date }}</div> 

console.log(json.stringify($scope.billing_history)) gives me following:

{   "may, 2015":{      "date":"may, 2015",      "timestamp":1432921230   },   "march, 2015":{      "date":"march, 2015",      "timestamp":1427846400   },   "february, 2015":{      "date":"february, 2015",      "timestamp":1425168000   } } 

no matter what, displayed:

february, 2015 march, 2015 may, 2015 

i've tried orderby:'-timestamp' , orderby:'+timestamp'

i'm not sure why isn't working. see going wrong?

you cannot use order-by filter object literal (or wont work expected). have object literal there no specific guaranteed ordering keys (and values). need convert array.

example:

angular.module('app', []).run(function($rootscope) {    $rootscope.billing_history = [{      "date": "may, 2015",      "timestamp": 1432921230    }, {      "date": "march, 2015",      "timestamp": 1427846400    }, {      "date": "february, 2015",      "timestamp": 1425168000    }]  })
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>  <div ng-app="app">    <p>reverse order:    <div ng-repeat="item in billing_history | orderby:'-timestamp'">{{ item.date }}</div>    <p>correct order:    <div ng-repeat="item in billing_history | orderby:'timestamp'">{{ item.date }}</div>  </div>

filter 1 option, careful, filters performance intensive (they run many times every digest cycle stabilize , in filter matters much) , operations on large object tricky. better set view model appropriately or convert format in controller itself.


Comments

Popular posts from this blog

Java 3D LWJGL collision -

methods - python can't use function in submodule -

c# - ErrorThe type or namespace name 'AxWMPLib' could not be found (are you missing a using directive or an assembly reference?) -