user344255
user344255

Reputation: 13

How to increment a counter variable as you loop through an ng-repeat?

If I have an ng-repeat that is looping through an array of 100 records. Let's say I have $scope.counter and I want to increment that value by 1 for each iteration through the ng-repeat. How can I inject a function wihtin the ng-repeat to increment the counter?

I need the counter so I can utilize it for a progress bar. The ng-repeat takes some time to complete, so I essentially want to graphically display the progress as the data loads.

This seemed like it would be pretty simple and straightforward, but I haven't seen anything online. That leads me to believe that I'm either doing something wrong or there's simply a better way of doing it.

Thanks!

Upvotes: 1

Views: 617

Answers (1)

Giovani Vercauteren
Giovani Vercauteren

Reputation: 1908

ngRepeat does everything in one digest cycle.

If your collection has 10 elements, those 10 elements will be processed from model to view in one cycle. If your collection has 100 elements, those 100 elements will be processed from model to view in one cycle.

There are no intermediate stages in the ngRepeat that allows you to use any counter system. You counter would simply go from 0 to 10 (or 100) in one digest cycle essentially proving no use.

You would have to create your own system that would process every element in one digest cycle, allowing you to use a counter system, but also creating a very expensive directive that would take up a lot of processing time.

Upvotes: 1

Related Questions