forked from angular/code.angularjs.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathangular.Array.count.html
53 lines (51 loc) · 2.54 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
<h1>angular.Array.count</h1>
<div class="angular-array-count"><h2>Description</h2>
<div class="description"><p>Determines the number of elements in an array. Provides an option for counting only those
elements for which a specified <code>condition</code> evaluates to <code>true</code>.</p>
<p>Note: This function is used to augment the <code>Array</code> type in Angular expressions. See
<a href="#!/api/angular.Array"><code>angular.Array</code></a> for more information about Angular arrays.</p></div>
<h2>Usage</h2>
<div class="usage"><div ng:non-bindable=""><pre class="brush: js; html-script: true;">angular.Array.count(array[, condition]);</pre>
</div>
<h3>Parameters</h3>
<ul class="parameters"><li><code ng:non-bindable="">array – {Array} – </code>
<p>The array containing the elements to be counted.</p></li>
<li><code ng:non-bindable="">condition<i>(optional)</i> – {(function()|string)} – </code>
<p>A function to be evaluated or
an Angular expression to be compiled and evaluated. The element being
iterated over is exposed to the <code>condition</code> as <code>this</code>.</p></li>
</ul>
<h3>Returns</h3>
<div class="returns"><code ng:non-bindable="">{number}</code>
– <p>Number of elements in the array. If a <code>condition</code> is specified, returns
the number of elements whose <code>condition</code> evaluates to <code>true</code>.</p></div>
</div>
<h2>Example</h2>
<div class="example"><doc:example>
<pre class="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>
</pre>
<pre class="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-live li:first-child').input('item.points').enter('23');
expect(binding('items.$count(\'points==1\')')).toEqual('1');
expect(binding('items.$count(\'points>1\')')).toEqual('2');
});
</pre>
</doc:example></div>
</div>