Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 33fd985

Browse files
committed
Merge pull request #1 from frankweb/orderby-notarray-documentation
Documentation to orderby when the parameter is not an array.
2 parents 3907b4c + 40de47f commit 33fd985

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
@ngdoc error
2+
@name orderBy:notarray
3+
@fullName Not an array
4+
@description
5+
6+
This error occurs when {@link ng.orderBy filter} is not used with an array:
7+
```html
8+
<input ng-model="search">
9+
<div ng-repeat="(key, value) in myObj | orderBy:search">
10+
{{ key }} : {{ value }}
11+
</div>
12+
```
13+
14+
orderBy must be used with an array so a subset of items can be returned.
15+
The array can be initialized asynchronously and therefore null or undefined won't throw this error.
16+
17+
To orderBy an object by the value of its properties you can create your own array based on that object:
18+
```js
19+
angular.module('aModule',[])
20+
.controller('aController', function($scope) {
21+
var obj = {
22+
one: {
23+
name: 'something 1'
24+
},
25+
two: {
26+
name: 'something 2'
27+
},
28+
three: {
29+
name: 'something 3'
30+
},
31+
};
32+
33+
//if using underscore
34+
$scope.objToArray = = _.values(obj);
35+
36+
});
37+
```
38+
That can be used as:
39+
```html
40+
<input ng-model="search">
41+
<div ng-repeat="some in objToArray | orderBy:'name'">
42+
{{ key }} : {{ value }}
43+
</div>
44+
```
45+
46+
You could as well convert the object to an array using a filter such as
47+
[toArrayFilter](https://github.com/petebacondarwin/angular-toArrayFilter):
48+
```html
49+
<input ng-model="search">
50+
<div ng-repeat="item in myObj | toArray:false | filter:search">
51+
{{ item }}
52+
</div>
53+
```

0 commit comments

Comments
 (0)