How to track cumulative index in 2 nested ng repeats

In view of the directive, I have two ng repeats. This directive creates a table, and the data of each cell in the table is from another data source. This data source needs a cumulative index of two repeaters.

selectedKandidaat.AntwoordenLijst[$index].Value

I only have the current $ index or $ parent. $ index. Therefore, in order to track the general index that I want to use in Antwoordenlijst [ $ index ], I will need to track the index variable. Can I just use the creation and updating of the index variable in the view.

Something like that:

..
  {{ totalIndex = 0 }}
  <tr ng-repeat="opgave in opgaven">
    <td ng-repeat="item in opgave.Items">
    {{ totalIndex += 1 }}
..

Actual code

<table class="table">
    <tr ng-repeat="opgave in opgaven">
        <td ng-repeat="item in opgave.Items">
            <select ng-model="selectedKandidaat.AntwoordenLijst[$index].Value"
                    ng-options="alternatief for alternatief in item.Alternatieven">           
            </select>
        </td>
    </tr>
</table>
+4
source share
1 answer

ng-repeat , , , .

<div ng-repeat="a in items1">
    parent: {{$index}}
    <div ng-repeat="b in items2" itemcell>
        child: {{$index}}
        total: {{totalCount}}
    </div>
</div>
total: {{totalCount}}

itemcell derective

    app.directives
    .directive('itemcell', function () {
        return {
        restrict: 'A',
            link: function ($scope, iElement, iAttrs) { 
                if (!$scope.$parent.$parent.totalCount) {
                $scope.$parent.$parent.totalCount = 0;
            }
            $scope.$parent.$parent.totalCount++;
        }
    }
    })

$parent. $index, , .

+3

All Articles