0

I have nested array so I had to use 3 times ng-repeat

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

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

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

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

So my problem is that I can't get access to 2nd ng-repeat $index , any ideas ?

2 Answers 2

4

This is the purpose of the ng-init directive

<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>

Alternatively use (index, value) formatting:

<div ng-repeat="(catIndex, cat) in cats">
   <div ng-repeat="kitty in cat">
        <button ng-click="delete(catIndex)">Delete</button>
   </div>
</div>
Sign up to request clarification or add additional context in comments.

4 Comments

or you could have used the (key, value) ng-repeat syntax?
@ryeballar - that's for objects, not arrays
You're wrong, it works for arrays as well. If the element being iterated in an ng-repeat is an array then the key is the index. Thus, you can do it like this, ng-repeat="(catIndex, cat) in cats"
You can add an update to your answer, at least the OP is aware of his options, you answer is correct but it adds N more times the watchers.
-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>

1 Comment

track by has nothing to do with $index

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.