Pumych Pumych - 5 months ago 11
AngularJS Question

Simple array output in angular

How to output this array as table with ng-repeat?

this.boardArr = [
[1,6,3],
[5,2,7],
[9,10,11],
]


This doesn't work:

<table class="game-board" ng-controller="GameBoardController as gameBoard">
<tr ng-repeat="row in boardArr">
<td ng-repeat="col in row">{{boardArr[row][col]}}</td>
</tr>
</table>

Answer

You have to create a table in your view like this:

<table class="game-board" ng-controller="GameBoardController as gameBoard">
    <tr ng-repeat="row in boardArr">
        <td ng-repeat="col in row">{{ col }}</td>
    </tr>
</table>

So ng-repeat will iterate over boardArr and then you can display the values in different cells like I did above. You don't have to access the value you want to display like this: boardArr[row][col] because colalready contains the value of boardArr[row][col].

Comments