AngularJS - 如何获取两个索引

8 javascript angularjs angularjs-ng-repeat

我有两个ng-repeat for child和parent divs如下

<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)
};
Run Code Online (Sandbox Code Playgroud)

我想得到孩子和父母索引.我该怎么取?

Ray*_*yon 8

使用 $parent.$index

每个ng-repeat都有自己的范围,并$index指的是最内层的范围ng-repeat

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

注意: </button>标签未关闭.