|
| 1 | +@ngdoc error |
| 2 | +@name orderBy:notarray |
| 3 | +@fullName Value is not array-like |
| 4 | +@description |
| 5 | + |
| 6 | +This error occurs when {@link ng.orderBy orderBy} is not passed an array-like value: |
| 7 | +```html |
| 8 | +<div ng-repeat="(key, value) in myObj | orderBy:someProp"> |
| 9 | + {{ key }} : {{ value }} |
| 10 | +</div> |
| 11 | +``` |
| 12 | + |
| 13 | +`orderBy` must be used with an array-like value so a subset of items can be returned. |
| 14 | +The array can be initialized asynchronously and therefore `null` or `undefined` won't throw this error. |
| 15 | + |
| 16 | +To use `orderBy` to order the properties of an object, you can create your own array based on that object: |
| 17 | +```js |
| 18 | +angular.module('aModule', []) |
| 19 | + .controller('aController', function($scope) { |
| 20 | + var myObj = { |
| 21 | + one: {id: 1, name: 'Some thing'}, |
| 22 | + two: {id: 2, name: 'Another thing'}, |
| 23 | + three: {id: 3, name: 'A third thing'} |
| 24 | + }; |
| 25 | + |
| 26 | + $scope.arrFromMyObj = Object.keys(myObj).map(function(key) { |
| 27 | + return myObj[key]; |
| 28 | + }); |
| 29 | + }); |
| 30 | +``` |
| 31 | +That can be used as: |
| 32 | +```html |
| 33 | +<label> |
| 34 | + Order by: |
| 35 | + <select ng-model="orderProp" ng-options="prop for prop in ['id', 'name']"></select> |
| 36 | +</label> |
| 37 | +<div ng-repeat="item in arrFromMyObj | orderBy:orderProp"> |
| 38 | + [{{ item.id }}] {{ item.name }} |
| 39 | +</div> |
| 40 | +``` |
| 41 | + |
| 42 | +You could as well convert the object to an array using a filter such as |
| 43 | +[toArrayFilter](https://github.com/petebacondarwin/angular-toArrayFilter): |
| 44 | +```html |
| 45 | +<label> |
| 46 | + Order by: |
| 47 | + <select ng-model="orderProp" ng-options="prop for prop in ['id', 'name']"></select> |
| 48 | +</label> |
| 49 | +<div ng-repeat="item in myObj | toArray:false | orderBy:orderProp"> |
| 50 | + [{{ item.id }}] {{ item.name }} |
| 51 | +</div> |
| 52 | +``` |
0 commit comments