View source Improve this doc

$filterProvider

service in module ng

Description

Filters are just functions which transform input to an output. However filters need to be Dependency Injected. To achieve this a filter definition consists of a factory function which is annotated with dependencies and is responsible for creating a filter function.

  1. // Filter registration
  2. function MyModule($provide, $filterProvider) {
  3. // create a service to demonstrate injection (not always needed)
  4. $provide.value('greet', function(name){
  5. return 'Hello ' + name + '!';
  6. });
  7.  
  8. // register a filter factory which uses the
  9. // greet service to demonstrate DI.
  10. $filterProvider.register('greet', function(greet){
  11. // return the filter function which uses the greet service
  12. // to generate salutation
  13. return function(text) {
  14. // filters need to be forgiving so check input validity
  15. return text && greet(text) || text;
  16. };
  17. });
  18. }

The filter function is registered with the $injector under the filter name suffix with Filter.

  1. it('should be the same instance', inject(
  2. function($filterProvider) {
  3. $filterProvider.register('reverse', function(){
  4. return ...;
  5. });
  6. },
  7. function($filter, reverseFilter) {
  8. expect($filter('reverse')).toBe(reverseFilter);
  9. });

For more information about how angular filters work, and how to create your own filters, see Filters in the Angular Developer Guide.

Methods

  • register(name, fn)

Register filter factory function.

Parameters

ParamTypeDetailsnameString

Name of the filter.

fnfunction

The filter factory function which is injectable.