7

I have a fairly large object that needs to be iterated over in nested ng-repeat loops

A simplified version looks like this:

{{totalEvents = 0}}
<div ng-repeat="(mainIndex, eventgroup) in EventsListings">

<ul>
    <li ng-repeat="event in eventgroup.events" ng-click="Current(totalEvents)">

    <div class="event-item-container" ng-show="eventDetailsView[totalEvents]">
        {{totalEvents = totalEvents + 1}}
    </div>

   </li>

</ul>

</div>
{{totalEvents = 0}

How can I keep track of totalEvents counter value.. How can I get a total number of iterations across nested loops IN the template?

2
  • do you need indexes? what is this condition for ? ng-show="eventDetailsView[totalEvents]" ? and this ng-click="Current(totalEvents)"? tell your goal our share your js or create plunker... Commented Mar 27, 2014 at 5:56
  • I need to toggle true and false for ng-show,, for element I hover over.. the problem I have is that for every outer iteration of the repeater indexes of inner iteration repeat and I can't seem to isolate the item I am over.. I am trying to attach something entirely unique to every iteration.. I am sure I am also just missing something obvious :/ Commented Mar 27, 2014 at 6:18

3 Answers 3

17

you can reach many value just using $index property of ng-repeat...

HTML

  <div ng-repeat="eventgroup in EventsListings" ng-init="outerIndex = $index">
    <ul>
      <li ng-repeat="event in eventgroup.events" ng-click="Current(totalEvents)">

        <div class="event-item-container">
          {{event.name}} can have unique value like 
          <br/>(outerIndex) * (eventgroup.events.length) + innerIndex
          <br/>Total Events = {{outerIndex * eventgroup.events.length + $index}}
          <br/>Inner Events = {{$index}}
        </div>

      </li>
    </ul>
  </div>

here is working PLUNKER

UPDATE

After some times and some comments I realized that my code is not calculating total iterations correctly, so I made some changes to fix it.

First mistake I made somehow I thought event numbers will be equals for every set, second one if there are more than 2 sets again it fails.

So for keeping track of total iterations I set an array which is called totalIterations in code. In this array I set total number events we already iterate so far,

For example at the finish of first set it will be length of first event group and for second it will be first and second group, and so on... For achieving this I used ng-repeat-end directive here is the final code

<div ng-repeat="eventgroup in EventsListings" ng-init="outerIndex = $index">
    <ul>
      <li ng-repeat="event in eventgroup.events">

        <div class="event-item-container">
          {{event.name}} can have unique value like
          <br/>Total Events Count = {{totalIterations[outerIndex- 1] + $index}}
          <br/>Innder Events = {{$index}}
        </div>

        <button class="btn btn-success" ng-click="Current({{totalIterations[outerIndex- 1] + $index}})">Current Event</button>

      </li>
      <span ng-repeat-end ng-init="totalIterations[outerIndex] = totalIterations[outerIndex - 1] + eventgroup.events.length"></span>
    </ul>
  </div>

and here is latest PLUNKER

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you ,, this helps a lot,, I need to look more into things I can do within templates. I appreciate your help.
by the way,, your plunker does not calculate total iterations correctly plnkr.co/edit/ZrfB4U84jkpT3Qm4QZCq?p=preview
if the elements length inside event not the same the logic gives messy result try the link above in @GRowing comment
5

I want to suggest different way to approach this, I think it's pretty cool and easy :

In the template :

<ul>
  <div ng-repeat="group in obj.objectGroups">
    <li>{{group.name}}</li>
    <li ng-repeat="item in group.items" ng-init="number = countInit()">    
      Total = {{number + 1}}
    </li>
  </div>
</ul>

In the controller :

$scope.totalCount = 0;
$scope.countInit = function() {
   return $scope.totalCount++;
}

Comments

1

If you really want the template to drive this calculation, you could keep a counter in the Controller and create a function that increments that counter. Then call that function from the template.

Honestly, though, it seems very strange to put this sort of logic in the view. It would make much more sense just to do a recursive count in pure Javascript in the Controller.

1 Comment

I am jsut trying to attach something entirely unique to every iteration. I am thinking that I am also just missing something obvious and that this is silly simple..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.