2

I have two nested ng-repeat and would like to access both the outer and inner indexes.

<div ng-repeat="column in columns" ng-init="columnIndex = $index">
     <div ng-repeat="row in column" ng-init="rowIndex = $index">
          <p ng-click="delete(column, row)">
              Delete
          </p>
     </div>
</div>

Now, this works fine as long as the outers model isn't updated. Apparently, ng-init initializes the values only once so columnIndex and rowIndex will not be update accordingly if I delete one element on the page.

Is there an other way to keep track of the outer and inner index so the correct values can be passed to the delete function?

6
  • 1
    stackoverflow.com/questions/18360841/… Commented May 5, 2015 at 14:55
  • @Ellery That other person wanted to modify the model on click. I don't, A specific value needs to be attributed to each delete function. Commented May 5, 2015 at 15:01
  • why do you need it in delete function? Easy to get indexing in controller with what you have and don't need $index Commented May 5, 2015 at 15:02
  • you need to use some uniqueIdentifier in your array itself, depening on $index i not good idea Commented May 5, 2015 at 15:02
  • @charlieftl Maybe I'm doing it wrong, but I need to know which value to delete from the model array. Commented May 5, 2015 at 15:03

1 Answer 1

3

You can set a name for each ng-repeat index:

<div ng-repeat="(columnIndex, column) in columns">
    <div ng-repeat="(rowIndex, row) in column">
        <p ng-click="delete(column, row)">
            Delete
        </p>
    </div>
</div>

Alternatively you can use $index for the inner index and $parent.$index for the outer index.

Sign up to request clarification or add additional context in comments.

1 Comment

I feel dumb, I didn't even think of the keys. But still, I didn't know about the $parent.$index, so thanks!

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.