1

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..

Shivan
  • 137
  • 1
  • 9
  • http://stackoverflow.com/questions/15316363/how-to-display-length-of-filtered-ng-repeat-data ... does it related?? – Moumit Feb 18 '16 at 08:27
  • Somehow yes, but he displays the data withing the scope, which is fine and easy. I have somehow take the amount of created 'li' elements by ng-repeat and present this value outside of the list itself(at top of page) – Shivan Feb 18 '16 at 08:37

1 Answers1

1
 <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 :)

  • Sorry, I just edited my post! This would work for object itself, but I need to reach nested values for each of object within. – Shivan Feb 18 '16 at 08:32
  • I changed my answer ! I'm not sure this code works since i did not run it, but it's the way to solve your issue :) – Jérémy PAUL Feb 18 '16 at 08:47
  • Thank you very much :) I'll try to tweak it a little bit, should work great. – Shivan Feb 18 '16 at 08:50