我有以下标记:
<div ng-repeat="department in storeData.departments">
<p>{{department.currentManager.fullName}}</p>
<select ng-model="department.currentManager" ng-options="user.fullName for user in department.users"></select>
</div>
使用以下数据模型:
department : {
currentManager: {
fullName: 'bob',
id: '2'
}
users: [
{
fullName: 'mike',
id: '1'
},
{
fullName: 'bob',
id: '2'
}
{
fullName: 'jeff',
id: '3'
}
]
但是select的默认选项始终为空白。这是因为ng-model不是直接在options数组中?我究竟做错了什么?
答案 0 :(得分:1)
<select ng-model="department.currentManager" ng-options="user.fullName for user in department.users"></select>
替换为
<select ng-model="department.currentManager.id" ng-options="user.id as user.fullName for user in department.users"></select>
<强> WORKING DEMO 强>
答案 1 :(得分:0)
有两种方式:
ng-options="user as user.fullName for user in department.users"
department.currentManager = department.users[1];
或者
ng-options="user.fullName for user in department.users"
department.currentManager = "bob";