Question

I'm using ng-repeat to create a list of entires and using a filter.

Is it possible to get the number of entries returned, like a length

    data-ng-repeat="entry in entries | filter: { country_code : countryCode }"
Was it helpful?

Solution

It is little bit tricky, use ng-init:

ng-init="filter_len = (entries | filter: { country_code : countryCode }).length" 

Example: http://jsfiddle.net/KJ3Nx/

OTHER TIPS

As you know filter responsible to "filter" the input list and it returns filtered list where objects have the same structure. Otherwise you get digest cycle reentering that causes Exceptions (aka > 10 cycles).

1st way

Get length after filtering:

<pre>{{(entries| myfilter:types).length }}</pre>

See Example

2nd way

Use custom filter and get length from there.

iApp.filter('myfilter', function() {

   return function( entries) {
    var filtered = [];

    angular.forEach(entries, function(entry) {
          filtered.push(entry);
    });

    // here fetch list length

    return filtered;
  };
});

I suppose the following code will work for you:

<p>Filtered number: {{(entries|filter:{country_code:countryCode}).length}}</p> <p>Total number: {{entries.length}}</p>

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top