简体   繁体   English

嵌套ng-repeat中的AngularJS计数

[英]AngularJS Count within nested ng-repeat

I am looking for a way to count the amount of 'td' elements created in a loop. 我正在寻找一种方法来计算循环中创建的“ td”元素的数量。

I can't use $index for this as on each row the index gets reset, what is the cleanest and simplest way to to set i on each iteration. 我不能为此使用$index ,因为在索引重置的每一行上,什么是在每次迭代中设置i的最干净,最简单的方法。 So the first column value is 1 & the first column count 0. 因此,第一列的值为1,第一列的计数为0。

My Code so far: 到目前为止,我的代码:

        <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>

and in my 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 ];

You can use ng-init . 您可以使用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>

Should be something like this: 应该是这样的:

<i class="icon ion-checkmark answer-correct" ng-if="submitted && answers[$parent.$parent.$index * 7 + $parent.$index].correct"></i>

Basically, take the $parents index (the one where you create the week) multiply it by 7 and then add the other $index in. I suggest outputting that until you're sure it's right. 基本上,将$ parents索引(创建星期的索引)乘以7,然后再添加另一个$ index。我建议将其输出,直到您确定它是正确的为止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM