简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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