javascript - AngularJS 嵌套 ng-repeat 内的计数

标签 javascript angularjs angularjs-ng-repeat

我正在寻找一种方法来计算循环中创建的“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/

相关文章:

javascript - 带倒计时显示的 SetInterval/SetTimeout

css - 选择子菜单时 AngularJS ng 类父菜单处于事件状态?

javascript - 在 ngClick 上获取数组中的下一项

javascript - AngularJS : directive inside ng-repeat not respecting new order

javascript - 停止 iOS7 放大 webview

javascript - 更新从 mongo 脚本打印到 StdOut(控制台)的 %

javascript - 作为参数的 javascript 参数和自定义选项之间有区别吗?

javascript - 将 Angular 表达式替换为字符串中的静态值

javascript - 子数组上的 ng-repeat 不起作用?

javascript - 当 $compile 下的 HTML 被更改时,ng-repeat 不起作用