使用ng-repeat来传播包含另一个ng-repeat列表的列表
Using ng-repeat to propagate a list containing another ng-repeat list
我正在尝试制作一个传播n个对象的角列表,并在列表项中,再次使用ng-repeat从对象的属性中创建一个水平列表这是一个数组
我如何做到这一点?
这是我目前正在处理的对象:
$scope.profileCompare = {
You: {
questionAnswer: [false, true, false, false, false, false],
questionImportance: [false, true, false]
},
Pizza: {
questionAnswer: [true, false, false, false, false, false],
questionImportance: [false, false, true]
},
Greenie: {
questionAnswer: [false, false, false, false, true, false],
questionImportance: [true, false, false]
}
}
在如何使用ng-repeat到达questionAnswer属性以在第一个ng-repeat的每个<li>
中创建水平列表方面没有实际进展。但是当我们进入
第一级不是数组,而是键值对的字典。
可以使用对象语法(key, value)
遍历第一层,然后使用数组语法遍历第二层;如下所示:
<table>
<tr ng-repeat="(key, value) in profileCompare">
<td>{{key}}</td>
<td ng-repeat="answerswer in value.questionAnswer track by $index">{{answer}}</td>
</tr>
</table>
在您的例子中,value
将是一个对象,具有2个属性,questionAnswer
和questionImportance
。
使用这种语法的缺点是顺序是由浏览器设置的,可能与数据集中定义的顺序相同,也可能不相同。如果你希望能够过滤、排序等数据,你需要首先将对象转换为数组;angular文档建议使用angular- toarrayfilter来完成这个任务。
如果要遍历第一层,则需要以下语法:
ng-repeat="(key, value) in profileCompare"
之后,可以很直接地遍历数组属性:
完整的HTML:
<div ng-repeat="(key, value) in profileCompare">
<h2>{{key}}</h2>
<h3>Answers</h3>
<ul>
<li ng-repeat="answerswer in value.questionAnswer track by $index">
{{answer}}
</li>
</ul>
<h3>Question importance</h3>
<ul>
<li ng-repeat="importance in value.questionImportance track by $index">
{{importance}}
</li>
</ul>
</div>
请注意$index的跟踪,因为您是在迭代布尔值,它肯定有重复项。
这是一个plunkr: http://plnkr.co/edit/U20nzazg0O9qnksFIze2?p=preview
相关文章:
- 将我的列表 vom ng-repeat 更改为收集重复 ng-click 后,无法正常工作
- AngularJS Dependent下拉列表,带ng repeat
- 使用ng repeat对列表进行分页
- 如何在angularjs中使用ng-repeat时将当前月份保持在列表顶部
- 根据本地存储中的列表过滤 ng-repeat
- AngularJS ng-repeat列表是动态构建的,$index始终为零
- AngularJs:我想从数组或列表中添加一个类,用于ng-repeat中的每次迭代
- 在 ng-repeat 中添加和删除 JSON 列表
- 添加重复键对象时如何更新 ng-repeat 列表中的元素
- 复选框列表 将数组格式化为 JSON 并在 ng-repeat 中映射后未填充
- Angular - ng-repeat - 获取 1 个列表中的所有 json 子数据
- AngularJs:选择在使用ng-repeat时不显示任何选项的下拉列表
- AngularJS嵌套ul列表和ng-repeat
- angularjs ng-repeat 中的动态互斥下拉列表
- 如何使用 Angular 更新 NG-Repeat 中的列表
- jQuery .focus() 无法使用 ng-repeat (AngularJS) 处理动态生成的列表项
- 推送到数组不会通过ng repeat指令更新视图列表
- 在视图中使用compile in指令和ng repeat的组合会阻止正确更新范围元素列表
- 在<ul>ng repeat生成的列表
- 如何使用ng repeat以缩略图格式创建一行中有n个项目的m个项目的列表