Nested ng-repeat for multidimensional array

后端 未结 1 1635
滥情空心
滥情空心 2021-01-05 04:10

I am trying to display a bi-dimensional array in html using the ng-repeat directive. I can display the first dimension ( table rows) but the second (table d

相关标签:
1条回答
  • 2021-01-05 04:41

    Working example:

    HTML :

    <!DOCTYPE html>
    <html ng-app="plunker">
    
    <head>
      <meta charset="utf-8" />
      <title>AngularJS Plunker</title>
      <script>
        document.write('<base href="' + document.location + '" />');
      </script>
      <script data-require="angular.js@1.4.x" src="https://code.angularjs.org/1.4.9/angular.js" data-semver="1.4.9"></script>
      <script src="app.js"></script>
    </head>
    
    <body ng-controller="MainCtrl">
      <p>Hello {{name}}!</p>
      <div>
        <table>
          <tr ng-repeat="y in grid">
            <td ng-repeat="x in y track by $index">{{x}}</td>
          </tr>
        </table>
      </div>
    </body>
    
    </html>
    

    JS:

    var app = angular.module('plunker', []);
    
    app.controller('MainCtrl', function($scope) {
      $scope.name = 'World';
      $scope.grid = [["empty", "empty", "empt", "empt", "empty"],
                      ["empty", "empty", "empt", "empt", "empty"]];
    });
    

    demo: http://plnkr.co/edit/yVC5nrH5Pv3Zzp8Py7FH?p=preview

    As your array data contains duplicate you want to add track by for unique id so i had added track by $index more about this https://docs.angularjs.org/error/ngRepeat/dupes

    0 讨论(0)
提交回复
热议问题