![](/img/trans.png)
[英]ng-options select list value set to the id of my choice and binding it correctly with ng-model
[英]Select not selecting when binding with ng-model and ng-options
这是我的选择:
<select class="form-control" ng-options="assistanceType as assistanceType.name for assistanceType in assistanceTypes" ng-model="selectedRecord.assistanceType"></select>
这是我用来加载援助类型的内容:
$scope.getAssistanceTypes = function () {
$http.get('/api/assistanceType/getAll').
success(function (data, status, headers, config) {
$scope.assistanceTypes = data;
}).
error(function (data, status, headers, config) {
alert(data.ExceptionMessage);
});
}
这是结果:
[
{
"assistanceTypeId": 1,
"name": "Essay"
},
{
"assistanceTypeId": 2,
"name": "Resume"
},
{
"assistanceTypeId": 3,
"name": "Test"
}
]
一切正常,我可以在更改选项时看到模型正在更新。
但是当我加载记录($ scope.selectedRecord)时,所选选项不会反映aidType对象!
这是“selectedRecord”:
{
"recordId": 1,
"student": {
"id": "xxx",
"firstName": "xxx",
"lastName": "xxx"
},
"createDate": "2015-03-04T15:35:40",
"closeDate": "2015-03-04T15:35:40",
"checkInDate": "2015-03-04T15:35:40",
"checkOutDate": "2015-03-04T15:35:40",
"consultant": {
"id": "xxx",
"firstName": "xxx",
"lastName": "xxx"
},
"assistanceType": {
"assistanceTypeId": 1,
"name": "Essay"
},
"course": {
"course": "xxx",
"name": "xxx",
"teacher": {
"id": "xxx",
"firstName": "xxx",
"lastName": "xxx"
}
},
"format": null,
"classStanding": null,
"comment": "Nothing here!"
}
我是AngularJS的新手,我很可能在这里遗漏了一些东西。 但是在我看来,在我加载主记录时,select应该填充selectedRecord.assistanceType中的对象。
有什么建议么?
谢谢!
问题是selectedRecord中的“assistanceType”对象与aidTypes数组中的等效实例不完全相同。 尝试这个:
<select class="form-control" ng-options="assistanceType as assistanceType.name for assistanceType in assistanceTypes track by assistanceType.name" ng-model="selectedRecord.assistanceType"></select>
请注意,我添加了“track by aidType.name”,以便它将按名称而不是对象的$$ hashkey进行比较。
选择element require name属性来获取模型元素选择,在你的情况下只需添加add name属性。 例如name =“aidType”。 除了它的要求之外,无需添加轨道。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.