我想使用ng-repeat为每个位置带来项目表。我无法使用ng-repeat来解决以下问题。请有人帮助我。在此先感谢。
结构是。
$scope.locations = [
// Location #1
[
{
location:"location1",
delar:"delar-x",
items:[
{
name:"Iphone",
price:124
},
{
name:"Blackbery",
price:132
}
]
},
{
location:"location1",
delar:"delar-y",
items:[
{
name:"Headphone",
price:10
},
{
name:"Cabels",
price:112
}
]
}
],
// Location #2
[
{
location:"location2",
delar:"delar-x",
items:[
{
name:"Iphone",
price:124
},
{
name:"Blackbery",
price:132
}
]
},
{
location:"location2",
delar:"delar-y",
items:[
{
name:"Headphone",
price:120
},
{
name:"Cabels",
price:110
}
]
}
]
]
我如何使用以下格式?
<div>
<h1> location1 </h1>
<table>
<tr>
<td> Iphone </td>
</tr>
<tr>
<td> Blackbery </td>
</tr>
<tr>
<td> Headphone </td>
</tr>
<tr>
<td> Cabels </td>
</tr>
</table>
<h1> location2 </h1>
<table>
<tr>
<td> Iphone </td>
</tr>
<tr>
<td> Blackbery </td>
</tr>
<tr>
<td> Headphone </td>
</tr>
<tr>
<td> Cabels </td>
</tr>
</table>
<div>
答案 0 :(得分:2)
您可以采用两步法
<div>
<h1 ng-repeat-start="loc in locs"> {{loc.location}} </h1>
<table ng-repeat-end>
<tr ng-repeat="item in loc.items">
<td>{{item.name}}</td>
</tr>
</table>
</div>
然后规范化阵列
$scope.locations = [....];
//normalize the locations array to a format which can be easily iterated using ng-repeat
var map = {}, array=[];
angular.forEach($scope.locations, function(obj){
angular.forEach(obj, function(obj){
var items = map[obj.location];
if(!items){
items = map[obj.location] = [];
array.push({
location: obj.location,
items:items
})
}
console.log(obj)
angular.forEach(obj.items, function(item){
items.push(item);
})
});
})
$scope.locs = array;
演示:Fiddle