AngularJS ng-repeat 数组 [英] AngularJS ng-repeat array of arrays

查看:56
本文介绍了AngularJS ng-repeat 数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这是我的观点:

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

这是我的控制器:

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

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

如何输出:

  • 1
  • 2
  • 3


  • 4
  • 5
  • 6


  • 7
  • 8
  • 9

解决方案

你的问题在于这一行:

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

item.itemsundefined 因为 item 是一个数组.

你应该枚举item而不是item.items:

<div ng-repeat="项目中的项目"><ul><li ng-repeat="i in item">{{i}}</li>

这是一个有效的 Plunk.

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

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

How can I output:

  • 1
  • 2
  • 3


  • 4
  • 5
  • 6


  • 7
  • 8
  • 9

解决方案

Your problem lies with this line:

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

item.items is undefined because item is an array.

You should enumerate item instead of 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.

这篇关于AngularJS ng-repeat 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆