-4

Another answers:

AngularJS multidimensional Array

Nested ng-repeat for multidimensional array

-

Original question:

How to create two dimensional 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>
1
  • are you familiar with the controller-view-model mechanism in angular? Commented Jun 23, 2016 at 8:53

3 Answers 3

2

E.g. by using nested ng-repeat directives like this

var app = angular.module('app', []);

app.controller('myController', function ($scope) {
  $scope.myArr = [
    [1,6,3],
    [5,2,7],
    [9,10,11],
  ];

});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="app">
  <div ng-controller="myController">
    <table>
      <tr>
        <th>optional heading 1</th>
        <th>optional heading 2</th>
        <th>optional heading 3</th>
      </tr>
      <tr ng-repeat="subArr in myArr">
        <td ng-repeat="val in subArr">{{ val }}</td>
      </tr>
    </table>
  </div>
</body>

Sign up to request clarification or add additional context in comments.

Comments

1

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

4 Comments

Dont Repeat Yourself :)
What's the problem?
Well I started to elaborate the answer when your question didn't include any code. You have the code edited above. I hope it helps you.
Your explanation is best way to show where is my mistake.
0

You'd have to loop over the rows and then their columns like this:

<table>
    <tr ng-repeat="row in $ctrl.boardArr">
        <td ng-repeat="col in row>{{col}}</td>
    </tr>
</table>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.