最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - AngularJS - How to get two indexes - Stack Overflow

programmeradmin2浏览0评论

I am having two ng-repeat for child and parent divs as follows

<div ng-repeat="stage in stages">
  <div ng-repeat="step in steps">
    <button ng-click="clickedStageAndStep($index)">
  </div>
</div>

$scope.clickedStageAndStep = function(index) {
  console.log("Step Index: " + index)
};

I wanna get child and parent indexes. How can I fetch?

I am having two ng-repeat for child and parent divs as follows

<div ng-repeat="stage in stages">
  <div ng-repeat="step in steps">
    <button ng-click="clickedStageAndStep($index)">
  </div>
</div>

$scope.clickedStageAndStep = function(index) {
  console.log("Step Index: " + index)
};

I wanna get child and parent indexes. How can I fetch?

Share Improve this question asked May 23, 2016 at 7:17 user4870812user4870812 2
  • 1 To access the index of the parent you have to go two steps up by writing $parent.$parent.$index See here: [ stackoverflow.com/questions/26875437/… ] – Avantika Saini Commented May 23, 2016 at 7:21
  • 1 Possible duplicate of passing 2 $index values within nested ng-repeat – James Brierley Commented May 23, 2016 at 12:31
Add a comment  | 

5 Answers 5

Reset to default 8

Use $parent.$index

Each ng-repeat has its own scope and $index refers to innermost scope of ng-repeat

$scope.clickedStageAndStep = function(parent, child) {
  console.log("Step Index: " + child);
};
<div ng-repeat="stage in stages">
  <div ng-repeat="step in steps">
    <button ng-click="clickedStageAndStep($parent.$index,$index)"></button>
  </div>
</div>

Note: </button> tag is not closed.

Define a variable that for parent index

    <div ng-repeat="stage in stages" ng-init="stageNumber = $index">
      <div ng-repeat="step in steps">
          <button ng-click="clickedStageAndStep($index, $parent.$index)" >YourButton </button>
      </div>
    </div>

    $scope.clickedStageAndStep = function(stageIndex,stepIndex) {
      console.log("Step Index: " + stepIndex + "And StageIndex: " + stageIndex)
    };

Use $parent.$index

HTML:

<div ng-app="app" ng-controller="ctrl">
  <div ng-repeat="stage in stages">
    <div ng-repeat="step in stage.steps">
      <span>Index: {{$index}}. ParentIndex:{{$parent.$index}}</span>
    </div>
  </div>
</div>

JS:

angular.module('app', []).
controller('ctrl', function($scope) {
  $scope.stages = [{
    steps: ['1', '2', '3']
  }, {
    steps: ['4', '5']
  },
  {steps:['6','7','8']}];
})

JSFIDDLE.

Use $index and $parent.$index to get child and parent indexes.

 <div ng-repeat="stage in stages">
      <div ng-repeat="step in steps">
        <button ng-click="clickedStageAndStep($parent.$index,$index)">
      </div>
    </div>

    $scope.clickedStageAndStep = function(stageIndex,stepIndex) {
      console.log("Stage Index: " + stageIndex + "And Step Index: " + stepIndex);
    };

Try

<div ng-init="parentIndex = $index"> </div>
发布评论

评论列表(0)

  1. 暂无评论