有棱角的将数据字典中的数据绑定到对象
Angular. Binding data from data dictionaries to objects
在angular中,我有几个集合。第一个是全球"产品属性"词典:
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>
我知道我可以迭代它,并将所有的"属性"数据复制到"汽车",但这会对我的数据产生大量无用的副本。还有其他想法吗
提前感谢:)
如果没有一种干净的数学方法来计算基于id
s的数组索引(就像您当前的示例中索引为(id
-1)),那么我会创建一个查找表,以数据库通常的方式将数组索引值映射到id
s
$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>
小提琴
相关文章:
- 在数据绑定中使用对象敲除绑定
- 挖空、视图模型位于其他对象和数据绑定中
- 如何使用Plates模板引擎将数据绑定到对象数组内的属性
- 有棱角的将数据字典中的数据绑定到对象
- 如果数据绑定对象只有它的名称,那么敲除如何知道它
- jQuery将数据绑定到动态创建的对象的单击事件
- 将数据绑定到基于对象属性的现有元素数
- 为什么对象上的“.show”属性会搞砸基于该对象的数据绑定
- 如何在数据绑定后在 KNOCKOUT js 中使用“with”访问另一个原型对象
- Javascript 对象数据绑定与 Vue
- D3.JS,如何在同一数据绑定中呈现多个对象
- d3通过id值将对象的数组数据绑定到加载的svg
- 我得到了一个JSON/Javascript对象,因为我可以在控制台中看到它,但我如何让它序列化并使我的数据绑定到我的Ui
- 聚合物网络组件数据绑定一个js对象/数组
- Grails控制器中的子对象数据绑定
- Angularjs指令隔离了对象不起作用的范围+单向数据绑定
- 了解如何使用element获取数据绑定键和值可观察对象
- Angularjs的数据绑定与数据属性对象
- 在动作方法中,如何将post数据绑定到动态对象
- 不能将Json数据绑定到d3.js对象