使用数组迭代对象属性

时间:2016-07-15 21:45:47

标签: javascript arrays angularjs object angularjs-ng-repeat

这是我的数据结构:

$scope.table = {
                    a: ["1","2","3","4"],
                    b: ["5","6","7","8"]
               };

我可以使用ng-repeat循环键:

<tr ng-repeat="(key,value) in table">
    <td>{{key}}</td> <!-- display a, b-->
    <td ng-repeat="???"></td>
</tr> 

我还想在键之后遍历values数组,我该怎么继续?

谢谢!

1 个答案:

答案 0 :(得分:3)

如果要在属性中循环遍历数组,可以执行以下操作:

angular.module('app', [])
  .controller('mainCtrl', function($scope) {
    $scope.table = {
                    a: ["1","2","3","4"],
                    b: ["5","6","7","8"]
               };
  });
<!DOCTYPE html>
<html ng-app="app">

<head>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.7/angular.min.js"></script>
</head>

<body ng-controller="mainCtrl">
  <table>
    <tr ng-repeat="(key, values) in table">
      <td ng-bind="key"></td>
      <td ng-repeat="value in values" ng-bind="value"></td>
    </tr>
  </table>
</body>

</html>