|
| 1 | +@ngdoc overview |
| 2 | +@name Filters |
| 3 | +@description |
| 4 | + |
| 5 | +A filter formats the value of an expression for display to the user. They can be used in view templates, |
| 6 | +controllers or services and it is easy to define your own filter. |
| 7 | + |
| 8 | +The underlying API is the {@link api/ng.$filterProvider filterProvider}. |
| 9 | + |
| 10 | +## Using filters in view templates |
| 11 | + |
| 12 | +Filters can be applied to expressions in view templates using the following syntax: |
| 13 | + |
| 14 | + {{ expression | filter }} |
| 15 | + |
| 16 | +E.g. the markup `{{ 12 | currency }}` formats the number 12 as a currency using the {@link api/ng.filter:currency `currency`} |
| 17 | +filter. The resulting value is `$12.00`. |
| 18 | + |
| 19 | +Filters can be applied to the result of another filter. This is called "chaining" and uses |
| 20 | +the following syntax: |
| 21 | + |
| 22 | + {{ expression | filter1 | filter2 | ... }} |
| 23 | + |
| 24 | +Filters may have arguments. The syntax for this is |
| 25 | + |
| 26 | + {{ expression | filter:argument1:argument2:... }} |
| 27 | + |
| 28 | +E.g. the markup `{{ 1234 | number:2 }}` formats the number 1234 with 2 decimal points using the |
| 29 | +{@link api/ng.filter:number `number`} filter. The resulting value is `1,234.00`. |
| 30 | + |
| 31 | + |
| 32 | +## Using filters in controllers and services |
| 33 | + |
| 34 | +You can also use filters in controllers and services. For this, add a dependency with the name `<filterName>Filter` |
| 35 | +to your controller or service. E.g. using the dependency `numberFilter` will inject the number filter. |
| 36 | +The injected argument is a function that takes the value to format as first argument and filter parameters |
| 37 | +starting with the second argument. |
| 38 | + |
| 39 | +The example below uses the filter called {@link api/ng.filter:filter `filter`}. |
| 40 | +This filter reduces arrays into sub arrays based on |
| 41 | +conditions. The filter can be applied in the view template with markup like |
| 42 | +`{{ctrl.array | filter:'a'}}`, which would do a fulltext search for "a". |
| 43 | +However, using a filter in a view template will reevaluate the filter on |
| 44 | +every digest, which can be costly if the array is big. |
| 45 | + |
| 46 | +The example below therefore calls the filter directly in the controller. |
| 47 | +By this, the controller is able to call the filter only when needed (e.g. when the data is loaded from the backend |
| 48 | +or the filter expression is changed). |
| 49 | + |
| 50 | +<doc:example module="FilterInControllerModule"> |
| 51 | +<doc:source> |
| 52 | +<script> |
| 53 | + angular.module('FilterInControllerModule', []). |
| 54 | + controller('FilterController', ['filterFilter', function(filterFilter) { |
| 55 | + this.array = [ |
| 56 | + {name: 'Tobias'}, |
| 57 | + {name: 'Jeff'}, |
| 58 | + {name: 'Brian'}, |
| 59 | + {name: 'Igor'}, |
| 60 | + {name: 'James'}, |
| 61 | + {name: 'Brad'} |
| 62 | + ]; |
| 63 | + this.filteredArray = filterFilter(this.array, 'a'); |
| 64 | + }]); |
| 65 | +</script> |
| 66 | + |
| 67 | +<div ng-controller="FilterController as ctrl"> |
| 68 | + <div> |
| 69 | + All entries: |
| 70 | + <span ng-repeat="entry in ctrl.array">{{entry.name}} </span> |
| 71 | + </div> |
| 72 | + <div> |
| 73 | + Entries that contain an "a": |
| 74 | + <span ng-repeat="entry in ctrl.filteredArray">{{entry.name}} </span> |
| 75 | + </div> |
| 76 | +</div> |
| 77 | +</doc:source> |
| 78 | +</doc:example> |
| 79 | + |
| 80 | + |
| 81 | +## Creating custom filters |
| 82 | + |
| 83 | +Writing your own filter is very easy: just register a new filter factory function with |
| 84 | +your module. Internally, this uses the {@link api/ng.$filterProvider `filterProvider`}. |
| 85 | +This factory function should return a new filter function which takes the input value |
| 86 | +as the first argument. Any filter arguments are passed in as additional arguments to the filter |
| 87 | +function. |
| 88 | + |
| 89 | +The following sample filter reverses a text string. In addition, it conditionally makes the |
| 90 | +text upper-case. |
| 91 | + |
| 92 | +<doc:example module="MyReverseModule"> |
| 93 | +<doc:source> |
| 94 | +<script> |
| 95 | + angular.module('MyReverseModule', []). |
| 96 | + filter('reverse', function() { |
| 97 | + return function(input, uppercase) { |
| 98 | + var out = ""; |
| 99 | + for (var i = 0; i < input.length; i++) { |
| 100 | + out = input.charAt(i) + out; |
| 101 | + } |
| 102 | + // conditional based on optional argument |
| 103 | + if (uppercase) { |
| 104 | + out = out.toUpperCase(); |
| 105 | + } |
| 106 | + return out; |
| 107 | + } |
| 108 | + }); |
| 109 | + |
| 110 | + function Ctrl($scope) { |
| 111 | + $scope.greeting = 'hello'; |
| 112 | + } |
| 113 | +</script> |
| 114 | + |
| 115 | +<div ng-controller="Ctrl"> |
| 116 | + <input ng-model="greeting" type="greeting"><br> |
| 117 | + No filter: {{greeting}}<br> |
| 118 | + Reverse: {{greeting|reverse}}<br> |
| 119 | + Reverse + uppercase: {{greeting|reverse:true}}<br> |
| 120 | +</div> |
| 121 | +</doc:source> |
| 122 | +</doc:example> |
| 123 | + |
0 commit comments