How to access the index of super parent in nested ng-repeats

天涯浪子 提交于 2020-01-06 06:36:08

问题


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>

回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/31006962/how-to-access-the-index-of-super-parent-in-nested-ng-repeats

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!