Determines the number of elements in an array. Provides an option for counting only those
elements for which a specified condition
evaluates to true
.
Note: This function is used to augment the Array
type in Angular expressions. See
angular.Array
for more information about Angular arrays.
angular.Array.count(array[, condition]);
array – {Array} –
The array containing the elements to be counted.
condition(optional) – {(function()|string)} –
A function to be evaluated or
an Angular expression to be compiled and evaluated. The element being
iterated over is exposed to the condition
as this
.
{number}
– Number of elements in the array. If a condition
is specified, returns
the number of elements whose condition
evaluates to true
.
<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>1')}}</em></p>
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'); });