Javascript AngularJS ng-repeat 数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31748612/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 07:07:34  来源:igfitidea点击:

AngularJS ng-repeat array of arrays

javascriptarraysangularjs

提问by Ryan

Is it possible to use ng-repeatwith an array of arrays?

是否可以ng-repeat与数组数组一起使用?

Here's my view:

这是我的观点:

<div ng-repeat="item in items">
  <p>{{item}}</p>
  <ul>
    <li ng-repeat="i in item.items">{{i}}</li>
  </ul>
</div>

Here's my controller:

这是我的控制器:

  var app = angular.module('plunker', []);
  app.controller('MainCtrl', function($scope) {
    $scope.items = [
      [1, 2, 3],
      [4, 5, 6],
      [7, 8, 9]
    ]
  });

Here's my Plunker:
http://plnkr.co/edit/b6vRVpUKkhPANNVXkkJL?p=preview

这是我的 Plunker:http://plnkr.co/edit/b6vRVpUKkhPANNVXkkJL?p=preview

How can I output:

我怎样才能输出:

  • 1
  • 2
  • 3
  • 1
  • 2
  • 3



  • 4
  • 5
  • 6
  • 4
  • 5
  • 6



  • 7
  • 8
  • 9
  • 7
  • 8
  • 9

采纳答案by Alex Booker

Your problem lies with this line:

你的问题在于这一行:

<li ng-repeat="i in item.items">{{i}}</li>

item.itemsis undefinedbecause itemis an array.

item.itemsundefined因为item是一个数组

You should enumerate iteminstead of item.items:

您应该枚举item而不是item.items

<body ng-controller="MainCtrl">
  <div ng-repeat="item in items">
    <ul>
      <li ng-repeat="i in item">{{i}}</li>
    </ul>
  </div>
</body>

Here's a working Plunk.

这是一个有效的Plunk

回答by Pierre-Alexandre Moller

You almost aleady have the result. It's just a little mistake in your second ng-repeat.

你几乎已经有结果了。这只是你第二次的一个小错误ng-repeat

<div ng-repeat="item in items">
  <p>{{item}}</p>
  <ul>
    <li ng-repeat="i in item">{{i}}</li>
  </ul>
</div>

You are already in item in your second ng-repeatyou don't need item.items.

你已经在ng-repeat你不需要的第二个项目中了item.items

There is the updated plunker : http://plnkr.co/edit/aLx05WWzFRVrocmXwr12?p=preview

有更新的plunker:http://plnkr.co/edit/aLx05WWzFRVrocmXwr12?p=preview