0

我正在重构一些代码,并希望将代码/名称对象数组更改为命名对象。我想这样做,以便可以将其用于相等性测试。旧的方法是直接比较代码......

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 ng-model="current.addressCode" ng-options="t.code as t.name for t in student.address"></select>

如何使用新对象生成选择表单元素?

4

1 回答 1

3

尝试这个

<select ng-model="current.addressCode" ng-options="value.code as value.name for (key,value) in student.address"></select>
于 2013-09-06T08:45:59.723 回答