给出以下对象:
{
key1: ["val1","val2","val3","val4"],
key2: ["val2","val5","val22","val4","val8","val1"],
key3: ["val1"],
key4: ["val11","val12"]
}
是否可以使用ng-repeat一次迭代所有值?
我的动机是:
1.我在<tr>
标签中使用ng-repeat,不能放在<tbody>
中,我需要在表格中为每个值创建一行,例如:
<tr>
<td>key1.val1</td>
</tr>
<tr>
<td>key1.val2</td>
</tr>
<tr>
<td>key1.val3</td>
</tr>
<tr>
<td>key1.val4</td>
</tr>
<tr>
<td>key2.val2</td>
</tr>
<tr>
<td>key2.val5</td>
</tr>
... and so on ...
任何想法?
谢谢!
答案 0 :(得分:3)
由于您的键是对象的属性,我会在第一步将其转换为数组:
$scope.keys = [];
for (var property in object) {
if (object.hasOwnProperty(property)) {
$scope.keys.push(object.property);
}
}
然后,您可以使用嵌套的ng-repeat指令显示所有值:
<div ng-repeat="key in keys">
<div ng-repeat="val in key[$index]">
{{val}}
修改:还有一种直接迭代对象属性的方法
<div ng-repeat="(key, value) in myObj"> ... </div>
与记录here:
一样答案 1 :(得分:1)
$scope.allValues = [];
angular.forEach(object, function(array) {
array.forEach(function(value) {
$scope.allValues.push(value);
});
});
然后只需在allValues
上使用ng-repeat。