javascript - AngularJS - 如何获得两个索引

标签 javascript angularjs angularjs-ng-repeat

我有两个 ng-repeat 子和父 div,如下所示

<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)
};

我想要获取子索引和父索引。如何获取?

最佳答案

使用 $parent.$index

每个ng-repeat有自己的范围和$index指的是 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>

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

关于javascript - AngularJS - 如何获得两个索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37384779/

相关文章:

javascript - 按下 shift 键和向左键或箭头键时突出显示字母

javascript - AngularJS 新路由器无法实例化 Controller

javascript - ng-根据 url 重复不同的值

javascript - ng-repeat中的ng-repeat,显示不正确

javascript - 外部 JavaScript 文件在 Rails 中不起作用

javascript - 在 promise 链中创建错误

javascript - Angular传单指令问题: other parts besides the map does't show the second time entering the page

javascript - 为ng-repeat添加的元素添加js

javascript - 有没有办法在不设置断点的情况下记录 JavaScript 堆栈跟踪?

javascript - iOS 8 中的 jQuery 窗口加载