AngularJS嵌套ng-repeat访问索引

时间:2014-07-30 16:06:52

标签: javascript angularjs angularjs-ng-repeat

我有嵌套数组,所以我不得不使用3次ng-repeat

    <div ng-repeat = "cat in cats"  >

          <div ng-repeat = "kitty in cat  "  >

             <button ng-click="delete($index)">Delete</button>

      </div>
   </div>
 </div>

所以我的问题是我无法访问第二次重复$ index,任何想法?

2 个答案:

答案 0 :(得分:4)

这是ng-init指令

的目的
<div ng-repeat="cat in cats" ng-init="catIndex = $index">
   <div ng-repeat="kitty in cat">
        <button ng-click="delete(catIndex)">Delete</button>
   </div>
</div>

或者使用(index, value)格式:

<div ng-repeat="(catIndex, cat) in cats">
   <div ng-repeat="kitty in cat">
        <button ng-click="delete(catIndex)">Delete</button>
   </div>
</div>

答案 1 :(得分:-2)

<div ng-repeat = "cat in cats track by $catIndex"  >

          <div ng-repeat = "kitty in cat track by $kittyIndex "  >

             <button ng-click="delete($kittyIndex)">Delete</button>

      </div>
</div>