AngularJS-通过dons't在ng模型中设置列表对象

AngularJS - select with track by doesn't set list object in ng-model

本文关键字:模型 ng 设置 对象 列表 dons 通过 AngularJS-      更新时间:2023-09-26

我有一个选择:

<select name="data_source" class="form-control" ng-model="data.data_source" ng-options="c.name for c in data_sources track by c.id"></select>

Data_sources数组中的Data_source对象的形式为

{id:"myid", name:"mydatasourcename", ...}

在init data.data_source中包含一个我从db 中检索到的对象

{id:"myid"}

select中显示了正确的值,但在我的ng模型中,我的原始data_source对象只有"id"属性。这里我想要的是来自data_sources数组的对象及其所有属性(name和other)。有办法做到这一点吗?

尽管在更改下拉列表时,您的ng模型将自动更新为所有属性。在init中,您可以按照以下方式更新数据源

data.data_source = dataFromSever
data_sources = dataListFromServer
//now get the matched obj of data_sources array by matching data_source.id
//set data_source = matchedObj.. This will ensure the correct structure of model at init