我正在寻找一种方法来计算循环中创建的“td”元素的数量。
我不能为此使用 $index
,因为在每一行上索引都会重置,在每次迭代中设置 i 的最干净、最简单的方法是什么。所以第一列值为 1,第一列计数为 0。
到目前为止我的代码:
<table class="calendar">
<thead>
<tr>
<th>M</th>
<th>T</th>
<th>W</th>
<th>T</th>
<th>F</th>
<th>S</th>
<th>S</th>
</tr>
</thead>
<tbody ng-click="bindCellValue($event)">
<tr ng-repeat="week in (days.length/7 | array)">
<td ng-repeat="day in days.slice(7*$index, 7*$index + 7) track by $index">
{{ day }}
<i class="icon ion-checkmark answer-correct" ng-if="submitted && answers[i].correct"></i>
<i class="icon ion-close answer-wrong" ng-if="submitted && !answers[i].correct"></i>
</td>
</tr>
</tbody>
</table>
在我的 Controller 中:
$scope.days = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, null, null, null, null ];
最佳答案
您可以使用ng-init
。
<tr ng-repeat="week in (days.length/7 | array)" ng-init="w = $index">
<td ng-repeat="day in days.slice(7*$index, 7*$index + 7) track by $index" ng-init="i = w*7 + $index">
{{ day }}
<i class="icon ion-checkmark answer-correct" ng-if="submitted && answers[i].correct"></i>
<i class="icon ion-close answer-wrong" ng-if="submitted && !answers[i].correct"></i>
</td>
</tr>
关于javascript - AngularJS 嵌套 ng-repeat 内的计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32376305/