From 380b9360f7bce5aef2e685481a52d11d9931bc07 Mon Sep 17 00:00:00 2001 From: Pablo Villoslada Puigcerber Date: Mon, 26 Jan 2015 15:54:00 +0100 Subject: [PATCH] docs(notarray): add error example and code blocks with suggested fixes Add code examples for the error and the suggested fixes. Followup of #10352. --- docs/content/error/filter/notarray.ngdoc | 47 +++++++++++++++++++++++- 1 file changed, 45 insertions(+), 2 deletions(-) diff --git a/docs/content/error/filter/notarray.ngdoc b/docs/content/error/filter/notarray.ngdoc index c46befba598c..4586424de118 100644 --- a/docs/content/error/filter/notarray.ngdoc +++ b/docs/content/error/filter/notarray.ngdoc @@ -3,6 +3,49 @@ @fullName Not an array @description -This error occurs when {@link ng.filter filter} is not used with an array. +This error occurs when {@link ng.filter filter} is not used with an array: +```html + +
+ {{ key }} : {{ value }} +
+``` + Filter must be used with an array so a subset of items can be returned. -The array can be initialized asynchronously so null or undefined won't throw this error. +The array can be initialized asynchronously and therefore null or undefined won't throw this error. + +To filter an object by the value of its properties you can create your own custom filter: +```js +angular.module('customFilter',[]) + .filter('custom', function() { + return function(input, search) { + if (!input) return input; + if (!search) return input; + var expected = ('' + search).toLowerCase(); + var result = {}; + angular.forEach(input, function(value, key) { + var actual = ('' + value).toLowerCase(); + if (actual.indexOf(expected) !== -1) { + result[key] = value; + } + }); + return result; + } + }); +``` +That can be used as: +```html + +
+ {{ key }} : {{ value }} +
+``` + +You could as well convert the object to an array using a filter such as +[toArrayFilter](https://github.com/petebacondarwin/angular-toArrayFilter): +```html + +
+ {{ item }} +
+```