forked from angular/code.angularjs.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathangular.Array.count.html
56 lines (54 loc) · 2.32 KB
/
angular.Array.count.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
<h1>angular.Array.count</h1>
<fieldset class="workInProgress">
<legend>Work In Progress</legend>
This page is currently being revised. It might be incomplete or contain inaccuracies.
</fieldset>
<h2>Description</h2>
<p>Determines the number of elements in an array. Optionally it will count only those elements
for which the <code>condition</code> evaluets to <code>true</code>.</p>
<p>Note: this function is used to augment the Array type in angular expressions. See
<a href="#!angular.Array"><code>angular.Array</code></a> for more info.</p>
<h2>Usage</h2>
<tt ng:non-bindable>
angular.Array.count(array, condition);
</tt>
<h3>Parameters</h3>
<ul>
<li><tt>array</tt> –
<tt>{Array}</tt>
<tt></tt>
– The array to count elements in.</li>
<li><tt>condition</tt> –
<tt>{(function()|string)=}</tt>
<tt></tt>
– A function to be evaluated or angular expression to be
compiled and evaluated. The element that is currently being iterated over, is exposed to
the <code>condition</code> as <code>this</code>.</li>
</ul>
<h3>Returns</h3>
<tt>{number}</tt> Number of elements in the array (for which the condition evaluates to true).
<h2>Example</h2>
<doc:example>
<doc:source>
<pre ng:init="items = [{name:'knife', points:1},
{name:'fork', points:3},
{name:'spoon', points:1}]"></pre>
<ul>
<li ng:repeat="item in items">
{{item.name}}: points=
<input type="text" name="item.points"/> <!-- id="item{{$index}} -->
</li>
</ul>
<p>Number of items which have one point: <em>{{ items.$count('points==1') }}</em></p>
<p>Number of items which have more than one point: <em>{{items.$count('points&gt;1')}}</em></p>
</doc:source>
<doc:scenario> it('should calculate counts', function() {
expect(binding('items.$count(\'points==1\')')).toEqual(2);
expect(binding('items.$count(\'points>1\')')).toEqual(1);
});
it('should recalculate when updated', function() {
using('.doc-example li:first-child').input('item.points').enter('23');
expect(binding('items.$count(\'points==1\')')).toEqual(1);
expect(binding('items.$count(\'points>1\')')).toEqual(2);
});</doc:scenario>
</doc:example>