|
3 | 3 | @fullName Not an array
|
4 | 4 | @description
|
5 | 5 |
|
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 | + |
7 | 14 | 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 | +``` |