What would be the best way to track the value of $ index in a ng repeat?

I have the following HTML:

<div class="row" ng-repeat="row in grid track by $index">
    <div cell class="cell" ng-repeat="cell in row track by $index" ng-click="game.addItem($index, $index)">
        {{cell.value}}
    </div>
</div>

I need to track by index in the first ng-repeat and in the second in order to pass them to addItem (). What would be the best way to get through both of them?

+4
source share
2 answers

I believe you can do:

<div class="row" 
     ng-repeat="row in grid track by $index" 
     ng-init="$rowIndex = $index">
  <div cell class="cell" 
       ng-repeat="cell in row track by $index" 
       ng-click="game.addItem($rowIndex, $index)">
    {{cell.value}}
  </div>
</div>

(Init may need to go to the external div, I can’t remember how I am sitting here)

Although the question will ask why you cannot just use the cell and row, what do you need indexes for for.

+6
source
<div class="row" ng-repeat="row in grid track by $index">
    <div cell class="cell" ng-repeat="cell in row track by $index" 
       ng-click="game.addItem(row, cell)">
        {{cell.value}}
    </div>
</div>
+2
source

Source: https://habr.com/ru/post/1546189/


All Articles