2014-10-17 22 views
0

我有一個選擇:AngularJS - 與軌道選擇通過不設置列表對象在NG-模型

<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", ...} 

在初始化data.data_source包含一個對象,我從DB

{id:"myid"} 

正確值顯示在選擇,但我的NG-模型I H檢索使用'id'屬性將我的原始data_source對象刪除。我在這裏想要的是來自data_sources數組及其所有屬性(名稱和其他)的對象。 有沒有辦法做到這一點?

+0

'NG-model'應該是'$ scope'變量,其中所選擇的值存儲,不是你在'NG-options'使用數據陣列。 – 2014-10-17 11:35:59

回答

0

雖然在更改下拉菜單中,您的ng模型會自動更新所有屬性。 在初始化,您可以在下面的方式更新您的DATA_SOURCE

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