Shivan
Shivan

Reputation: 137

Show number of ng-repeat items outside of its scope

I'm repeating through list of JSON object(nested) items like this :

 <div ng-repeat='item in items'>
  <ul>
     <li ng-repeat='specific in item'>{{specific}}</li>
  </ul>
</div> 

I would like to present amount of 'specific' items in each item object at the top of the page(outside of ng-repeat scope). I cannot use items.length, becuase I would like to display amount of specific items(it has to be looped so), but I don't know how to bind the data from ng-repeat and use it outside it's scope..

Upvotes: 1

Views: 288

Answers (1)

J&#233;r&#233;my PAUL
J&#233;r&#233;my PAUL

Reputation: 34

 <span>Number of items :  {{countItems(items.length)}}</span>

$scope.countItems = function(items) {
     var count = 0;
     for(var item in items) {
         for(var specific in item) {
             count++;
         }
         // Instead of the second for, you can just write : 
         // count = count + item.length;
     }
     return count;
};

It should do the trick :)

Upvotes: 1

Related Questions