summaryrefslogtreecommitdiffstats
path: root/3rdparty/js/angular-1.0.2/docs/partials/api/ng.$filterProvider.html
blob: ef637d620177d3b7554b084209edc4ff9ee9211a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<h1><code ng:non-bindable="">$filterProvider</code>
<span class="hint">(service in module <code ng:non-bindable="">ng</code>
)</span>
</h1>
<div><h2 id="Description">Description</h2>
<div class="description"><p>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 the filter function.</p>

<pre class="prettyprint linenums">
  // Filter registration
  function MyModule($provide, $filterProvider) {
    // create a service to demonstrate injection (not always needed)
    $provide.value('greet', function(name){
      return 'Hello ' + name + '!';
    });

    // register a filter factory which uses the
    // greet service to demonstrate DI.
    $filterProvider.register('greet', function(greet){
      // return the filter function which uses the greet service
      // to generate salutation
      return function(text) {
        // filters need to be forgiving so check input validity
        return text && greet(text) || text;
      };
    });
  }
</pre>

<p>The filter function is registered with the <code>$injector</code> under the filter name suffixe with <code>Filter</code>.
<pre class="prettyprint linenums">
  it('should be the same instance', inject(
    function($filterProvider) {
      $filterProvider.register('reverse', function(){
        return ...;
      });
    },
    function($filter, reverseFilter) {
      expect($filter('reverse')).toBe(reverseFilter);
    });
</pre>

<p>For more information about how angular filters work, and how to create your own filters, see
<a href="guide/dev_guide.templates.filters">Understanding Angular Filters</a> in the angular Developer
Guide.</p></div>
<div class="member method"><h2 id="Methods">Methods</h2>
<ul class="methods"><li><h3 id="register">register(name, fn)</h3>
<div class="register"><p>Register filter factory function.</p><h4 id="Parameters">Parameters</h4>
<ul class="parameters"><li><code ng:non-bindable="">name – {String} – </code>
<p>Name of the filter.</p></li>
<li><code ng:non-bindable="">fn – {function} – </code>
<p>The filter factory function which is injectable.</p></li>
</ul>
</div>
</li>
</ul>
</div>
</div>