bdh
bdh

Reputation: 127

AngularJS: Count iterations of ng-repeat

Is it possible to use ng-repeat to make a grid of consecutive numbers?

I can use two ng-repeats to make a grid like this:

<table>
  <tr ng-repeat="c in [1, 2, 3]">
    <td ng-repeat="r in [1, 2, 3]">
            {{c * r}}
        </td>
    </tr>
</table>

Which outputs this:

1    2    3
2    4    6
3    6    9

But the output I want is:

1    2    3
4    5    6
7    8    9

Or is there a more appropriate angular directive I could use?

Upvotes: 2

Views: 193

Answers (1)

William
William

Reputation: 2935

<table>
  <tr ng-repeat="c in [0, 1, 2]">
    <td ng-repeat="r in [1, 2, 3]">
            {{c * 3 + r}}
        </td>
    </tr>
</table>

Or, if you don't want to change the first array:

<table>
  <tr ng-repeat="c in [1, 2, 3]">
    <td ng-repeat="r in [1, 2, 3]">
            {{(c-1) * 3 + r}}
        </td>
    </tr>
</table>

Upvotes: 3

Related Questions