在角度我有很少的收藏。第一个是全球“产品属性”字典:
attributes : [
{id:1, value:"red", type:"color"},
{id:2, value:"green", type:"color"},
{id:3, value:"big", type:"size"}
]
我有一个'产品'对象:
cars : [{
name : "red big car",
attributes : [1, 3] # id's corresponding to 'attributes' list
}]
现在在我的模板中,我希望以一种简单的方式访问该数据:
<div ng-repeat='car in cars'>
<p>{{ car.name }}</p>
<p ng-repeat='attribute in attributes'>{{ attribute.value }}</p>
</div>
获得结果:
<div ng-repeat='car in cars'>
<p>red big car</p>
<p ng-repeat='attribute in attributes'>red</p>
<p ng-repeat='attribute in attributes'>big</p>
</div>
我知道我可以遍历它并将所有'属性'数据复制到'汽车',但它会使我的数据无数副本。还有其他想法吗?
在此先感谢:)
答案 0 :(得分:1)
如果没有基于id
计算数组索引的干净数学方法(就像当前索引为(id
- 1)的例子)那么我会创建一个映射数组的查找表索引值到id
的方式与数据库通常的方式相同。像这样:
$scope.lookup = {1:0,
2:1,
3:2 }
然后使用它将car
属性数组索引映射到数组索引到属性数组中:
<div ng-repeat='car in cars'>
<p>{{ car.name }}</p>
{{car.attributes}}
<p ng-repeat='indx in car.attributes'>{{attributes[lookup[indx]].value }}</p>
</div>