Angularjs选择 - 根据模型设置初始选择

时间:2014-01-09 20:09:28

标签: arrays angularjs

我有一组从服务器中提取的作业对象,并且还从服务器中提取了一系列人物对象。我希望person作业由一个select元素表示,从job数组中填充。我使用ng-options填充了选择,并且在更改选择时,更新了人物模型的作业对象。我唯一的问题是,从select开始显示一个空值。我无法弄清楚如何让它显示人物当前的工作?

 <select  
   ng-model='person.job' 
   ng-options='job.title for job in jobs'>
 </select>

person对象看起来像这样

{
  "id": "1",
  "firstName": "Marianne",
  "lastName": "Jenkins",
  "middleNames": null,
  "ext": "4680",
  "phoneCell": "1-174-668-3846",
  "phoneHome": "+10(2)5744088105",
  "takerNumber": "180",
  "hidden": "0",
  "created_at": "2014-01-09 12:55:12",
  "updated_at": "2014-01-09 12:55:12",
  "job": {
    "id": "25",
    "title": "Office Manager",
    "created_at": "2014-01-09 12:55:11",
    "updated_at": "2014-01-09 19:25:03"
  },
  "office": {
    "id": "4",
    "name": "Salt Lake City",
    "prefix": "702",
    "order": "2",
    "takerNumber": "103",
    "address_id": "1"
  }
}

并且jobs数组看起来像这样

[
  {
    "id": "1",
    "title": "Field Service Tech",
    "created_at": "2014-01-09 12:55:11",
    "updated_at": "2014-01-09 19:25:03"
  },
  {
    "id": "2",
    "title": "Inside Sales Manager",
    "created_at": "2014-01-09 12:55:11",
    "updated_at": "2014-01-09 19:25:03"
  },
  {
    "id": "3",
    "title": "Office Assistant",
    "created_at": "2014-01-09 12:55:11",
    "updated_at": "2014-01-09 19:25:03"
  },
  ...
  {
    "id": "25",
    "title": "Office Manager",
    "created_at": "2014-01-09 12:55:11",
    "updated_at": "2014-01-09 19:25:03"
  }
  ...
]

希望我提供了足够的信息,如果我的描述有点不清楚,我很抱歉,我在这里努力准确地描述我的问题。

提前致谢!

注意:此处显示的人是生成的,不是真实的。

1 个答案:

答案 0 :(得分:1)

请试试这个:

<select  
   ng-model='person.job' 
   ng-options='job.title for job in jobs track by job.id'>
</select>

e.g。使用:track by job.id

扩展表达式