/
ng.filter:filter.html
121 lines (113 loc) · 6.08 KB
/
ng.filter:filter.html
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<h1><code ng:non-bindable="">filter</code>
<span class="hint">(filter in module <code ng:non-bindable="">ng</code>
)</span>
</h1>
<div><a href="http://github.com/angular/angular.js/edit/master/src/ng/filter/filter.js" class="improve-docs btn btn-primary">Improve this doc</a><h2 id="Description">Description</h2>
<div class="description"><p>Selects a subset of items from <code>array</code> and returns it as a new array.</p>
<p>Note: This function is used to augment the <code>Array</code> type in Angular expressions. See
<a href="api/ng.$filter"><code>ng.$filter</code></a> for more information about Angular arrays.</p></div>
<h2 id="Usage">Usage</h2>
<div class="usage"><h3 id="In.HTML.Template.Binding">In HTML Template Binding</h3>
<div class="in-html-template-binding"><code ng:non-bindable="">{{ filter_expression | filter:expression:comparator }}</code>
</div>
<h3 id="In.JavaScript">In JavaScript</h3>
<div class="in-javascript"><code ng:non-bindable="">$filter('filter')(array, expression, comparator)</code>
</div>
<h3 id="Parameters">Parameters</h3>
<ul class="parameters"><li><code ng:non-bindable="">array – {Array} – </code>
<p>The source array.</p></li>
<li><code ng:non-bindable="">expression – {string|Object|function()} – </code>
<p>The predicate to be used for selecting items from
<code>array</code>.</p>
<p>Can be one of:</p>
<ul>
<li><p><code>string</code>: Predicate that results in a substring match using the value of <code>expression</code>
string. All strings or objects with string properties in <code>array</code> that contain this string
will be returned. The predicate can be negated by prefixing the string with <code>!</code>.</p></li>
<li><p><code>Object</code>: A pattern object can be used to filter specific properties on objects contained
by <code>array</code>. For example <code>{name:"M", phone:"1"}</code> predicate will return an array of items
which have property <code>name</code> containing "M" and property <code>phone</code> containing "1". A special
property name <code>$</code> can be used (as in <code>{$:"text"}</code>) to accept a match against any
property of the object. That's equivalent to the simple substring match with a <code>string</code>
as described above.</p></li>
<li><p><code>function</code>: A predicate function can be used to write arbitrary filters. The function is
called for each element of <code>array</code>. The final result is an array of those elements that
the predicate returned true for.</p></li>
</ul></li>
<li><code ng:non-bindable="">comparator – {function(expected, actual)|true|undefined} – </code>
<p>Comparator which is used in
determining if the expected value (from the filter expression) and actual value (from
the object in the array) should be considered a match.</p>
<p>Can be one of:</p>
<ul>
<li><p><code>function(expected, actual)</code>:
The function will be given the object value and the predicate value to compare and
should return true if the item should be included in filtered result.</p></li>
<li><p><code>true</code>: A shorthand for <code>function(expected, actual) { return angular.equals(expected, actual)}</code>.
this is essentially strict comparison of expected and actual.</p></li>
<li><p><code>false|undefined</code>: A short hand for a function which will look for a substring match in case
insensitive way.</p></li>
</ul></li>
</ul>
</div>
<h2 id="Example">Example</h2>
<div class="example"><h4>Source</h4>
<div source-edit="" source-edit-deps="angular.js" source-edit-html="index.html-192" source-edit-css="" source-edit-js="" source-edit-unit="" source-edit-scenario="scenario.js-193"></div>
<div class="tabbable"><div class="tab-pane" title="index.html">
<pre class="prettyprint linenums" ng-set-text="index.html-192" ng-html-wrap=" angular.js"></pre>
<script type="text/ng-template" id="index.html-192">
<div ng-init="friends = [{name:'John', phone:'555-1276'},
{name:'Mary', phone:'800-BIG-MARY'},
{name:'Mike', phone:'555-4321'},
{name:'Adam', phone:'555-5678'},
{name:'Julie', phone:'555-8765'},
{name:'Juliette', phone:'555-5678'}]"></div>
Search: <input ng-model="searchText">
<table id="searchTextResults">
<tr><th>Name</th><th>Phone</th></tr>
<tr ng-repeat="friend in friends | filter:searchText">
<td>{{friend.name}}</td>
<td>{{friend.phone}}</td>
</tr>
</table>
<hr>
Any: <input ng-model="search.$"> <br>
Name only <input ng-model="search.name"><br>
Phone only <input ng-model="search.phone"å><br>
Equality <input type="checkbox" ng-model="strict"><br>
<table id="searchObjResults">
<tr><th>Name</th><th>Phone</th></tr>
<tr ng-repeat="friend in friends | filter:search:strict">
<td>{{friend.name}}</td>
<td>{{friend.phone}}</td>
</tr>
</table>
</script>
</div>
<div class="tab-pane" title="End to end test">
<pre class="prettyprint linenums" ng-set-text="scenario.js-193"></pre>
<script type="text/ng-template" id="scenario.js-193">
it('should search across all fields when filtering with a string', function() {
input('searchText').enter('m');
expect(repeater('#searchTextResults tr', 'friend in friends').column('friend.name')).
toEqual(['Mary', 'Mike', 'Adam']);
input('searchText').enter('76');
expect(repeater('#searchTextResults tr', 'friend in friends').column('friend.name')).
toEqual(['John', 'Julie']);
});
it('should search in specific fields when filtering with a predicate object', function() {
input('search.$').enter('i');
expect(repeater('#searchObjResults tr', 'friend in friends').column('friend.name')).
toEqual(['Mary', 'Mike', 'Julie', 'Juliette']);
});
it('should use a equal comparison when comparator is true', function() {
input('search.name').enter('Julie');
input('strict').check();
expect(repeater('#searchObjResults tr', 'friend in friends').column('friend.name')).
toEqual(['Julie']);
});
</script>
</div>
</div><h4>Demo</h4>
<div class="well doc-example-live animator-container" ng-embed-app="" ng-set-html="index.html-192" ng-eval-javascript=""></div></div>
</div>