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

Commit 2ece1c9

Browse files
Puigcerberpkozlowski-opensource
authored andcommittedJan 27, 2015
docs(notarray): add error example and code blocks with suggested fixes
Add code examples for the error and the suggested fixes. Followup of #10352. Closes #10872
1 parent 7602cd5 commit 2ece1c9

File tree

1 file changed

+45
-2
lines changed

1 file changed

+45
-2
lines changed
 

‎docs/content/error/filter/notarray.ngdoc

+45-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,49 @@
33
@fullName Not an array
44
@description
55

6-
This error occurs when {@link ng.filter filter} is not used with an array.
6+
This error occurs when {@link ng.filter filter} is not used with an array:
Has conversations. Original line has conversations.
7+
```html
8+
<input ng-model="search">
9+
<div ng-repeat="(key, value) in myObj | filter:search">
10+
{{ key }} : {{ value }}
11+
</div>
12+
```
13+
714
Filter must be used with an array so a subset of items can be returned.
8-
The array can be initialized asynchronously so null or undefined won't throw this error.
15+
The array can be initialized asynchronously and therefore null or undefined won't throw this error.
16+
17+
To filter an object by the value of its properties you can create your own custom filter:
18+
```js
19+
angular.module('customFilter',[])
20+
.filter('custom', function() {
21+
return function(input, search) {
22+
if (!input) return input;
23+
if (!search) return input;
24+
var expected = ('' + search).toLowerCase();
25+
var result = {};
26+
angular.forEach(input, function(value, key) {
27+
var actual = ('' + value).toLowerCase();
28+
if (actual.indexOf(expected) !== -1) {
29+
result[key] = value;
30+
}
31+
});
32+
return result;
33+
}
34+
});
35+
```
36+
That can be used as:
37+
```html
38+
<input ng-model="search">
39+
<div ng-repeat="(key, value) in myObj | custom:search">
40+
{{ key }} : {{ value }}
41+
</div>
42+
```
43+
44+
You could as well convert the object to an array using a filter such as
45+
[toArrayFilter](https://github.com/petebacondarwin/angular-toArrayFilter):
46+
```html
47+
<input ng-model="search">
48+
<div ng-repeat="item in myObj | toArray:false | filter:search">
49+
{{ item }}
50+
</div>
51+
```

0 commit comments

Comments
 (0)
This repository has been archived.