0

I am looking for a way to count the amount of 'td' elements created in a loop.

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. So the first column value is 1 & the first column count 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 ];
5
  • You mean days.length? Anyway, you know the number beforehand. It's based on the model, so why count something? Commented Sep 3, 2015 at 13:09
  • 1
    because I need to use the count of the items to match up with the key in an answers array: ng-if="submitted && answers[i].correct"> Commented Sep 3, 2015 at 13:12
  • Can you create a plunker or jsfiddle for the same? Commented Sep 3, 2015 at 13:13
  • I'm not quite sure I get the question. Do you want the current count for each cell? Can you maybe draw or make a quick static table of how you want it to function? Commented Sep 3, 2015 at 13:15
  • Each ng-repeat creates a child scope with the passed data, and also adds an additional $index variable in that scope.So what you need to do is reach up to the parent scope, and use that $index. See this Commented Sep 3, 2015 at 13:33

2 Answers 2

2

You can use 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>
Sign up to request clarification or add additional context in comments.

Comments

0

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.

2 Comments

ng-if creates a new scope. So where, do you think, does $parent point to?
Ah yes, just glossed over that, so double $parent

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.