我正在重构一些代码,并希望将代码/名称对象数组更改为命名对象。我想这样做,所以我可以用它来进行相等测试。旧方法是直接比较代码......
if(current.addressCode === "1"){ ... }
但我更愿意这样做......
if(current.addressCode === types.student.address.letter.code){ ... }
旧物体看起来像这样......
student{
address: [
{code: "0", name: "Select proof of address"},
{code: "1", name: "Letter"},
{code: "3", name: "Photograph"}
],
id: [
{code: "0", name: "Select type of ID"},
{code: "1", name: "Passport"},
{code: "2", name: "Driving Licence"}
]
}
所以我希望我的新对象看起来像这样......
student: {
address: {
select: {code: "0", name: "Select proof of address"},
letter: {code: "1", name: "Letter"},
photograph: {code: "3", name: "Photograph"},
},
id: {
select: {code: "0", name: "Select type of ID"},
passport: {code: "1", name: "Passport"},
drivingLicense: {code: "2", name: "Driving Licence"},
}
}
我遇到困难的时候,我不知道如何从这个对象生成角度js中的select元素,这个元素过去是从这样的旧结构生成的......
<select ng-model="current.addressCode" ng-options="t.code as t.name for t in student.address"></select>
如何使用新对象生成选择表单元素?
答案 0 :(得分:3)
试试这个
<select ng-model="current.addressCode" ng-options="value.code as value.name for (key,value) in student.address"></select>