javascript - ng-repeat 数组的关联数组?

标签 javascript arrays angularjs angularjs-ng-repeat

我正在尝试使用数组的关联数组以 Angular 设置嵌套重复。例如我有这样的结构:

集合['key1'] = [obj1,obj2,obj3,obj4];

集合['key2'] = [obj5,obj6,obj7];

我想要一个结构如下的 View :

<div ng-repeat="collection in collections">
  <h4>{{collection.id}}</h4>
  <div ng-repeat="item in collection">
     <span>{{item.name | item.value}}</span>
  </div>
</div>

但是,一旦我添加 ng-repeat="collection in collections"我的 View 就变成空白。有没有办法以 Angular 来做到这一点,或者如果我想以这种方式循环它,我是否需要更新存储数据的方式?谢谢。

最佳答案

只是给你一个如何使用它的例子

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.school = {
      classes:[
        {name:"Class 1", peoples:["Peter","Sue","Marc"]},
        {name:"Class 2", peoples:["John","Edward","Sara"]}
      ]
    }
});
<!DOCTYPE html>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="myCtrl">
 
<div ng-repeat="class in school.classes">
  <h4>{{class.name}}</h4>
  <div ng-repeat="person in class.peoples">
    <label>{{person}}</label>
  </div>
  <br>
</div>
</div>

</body>
</html>

关于javascript - ng-repeat 数组的关联数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37400107/

相关文章:

javascript - 如果 ul 为空则隐藏 div

php - SQL join into join 创建好数组

javascript - 获得第一个单词在位置 0 和其他单词在位置 1 的数组作为输出的最佳方法?

python - 如何制作一个与给定数组大小相同的新 numpy 数组并用标量值填充它

javascript - AngularJS:如何以 Angular 隐藏单击的元素?

Javascript(NodeJs) - 将数组和对象转换为批量插入 mysql(Sequelize)

javascript - 在Datatable中渲染数组数据

javascript - meteor 列表项目数量

angularjs - SVG 未加载 Angular 2

angularjs - Ionic v1 中的 img src 路径