0

I want to access the index of the outermost ng-repeat.

<div ng-repeat="">
  <div ng-repeat="">
    <div ng-repeat="">
      //I want to get the $index of outer ng-repeat here
    </div>
  </div>
</div>
  • possible duplicate of [Access index of the parent ng-repeat from child ng-repeat](http://stackoverflow.com/questions/14807258/access-index-of-the-parent-ng-repeat-from-child-ng-repeat) – Martin Jun 23 '15 at 15:28

2 Answers2

3

Use ng-init to set the outer index:

<div ng-repeat="" ng-init="outerIndex = $index">

Then you can use outerIndex in the other scopes.

Angular documentation has this example specifically

Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
0

A nice clean way to do this is with ng-init

<div ng-repeat="" ng-init="indxFirst = $index">
 <div ng-repeat="" ng-init="indxSecond = $index">
   <div ng-repeat="">
       //the current index $index
      //indxFirst is your the parent most index
      <div ng-click="myClick(indxFirst)">Check most outer parent index</div>
   </div>
 </div>
</div>

So, for example I added a click function and passed it inside the inner most repeat, and in the controller you would see it passed here like :

$scope.myClick = function(index){
   console.log("parent most index is", index);
};

Can use it however you like, this is just an example.

ajmajmajma
  • 13,712
  • 24
  • 79
  • 133