2014-10-07 65 views
0

我綁定選擇元素的數據,看起來像這樣:AngularJS選擇和ng選項:我可以綁定和反對ng模型,只顯示一個屬性?

var gearConditions = [ 
    { id: 1, value: 'New' }, 
    { id: 2, value: 'Like New' }, 
    { id: 3, value: 'Excellent' }, 
    { id: 4, value: 'Good' }, 
    { id: 5, value: 'Fair' }, 
    { id: 6, value: 'Very Used' }, 
    { id: 7, value: 'Other' } 
]; 

我選擇這個樣子的:

<select name="condition" 
      class="form-control" 
      ng-model="postModel.condition" 
      ng-options="condition.id as condition.value for condition in gearConditions"> 
    </select> 

這種結合condition.id到NG-模型和顯示狀態。值作爲選擇選項。我的問題是:我能保持當前的行爲但將整個對象綁定到ng模型嗎?

例如,如果第一個選項被選中,那麼模型值將是{ID:1,值:「新建」},而不是僅僅1

這是可能的,或者我需要用NG-改變完成這個?

謝謝!

回答

1

是,改變condition.idcondition

ng-options="condition as condition.value for condition in gearConditions" 
+0

哦:粗糙!謝謝! – Nick 2014-10-07 21:17:56

0

我想你,你嘗試類似:

// I took this from my code 
ng-options="key as value for (key, value) in ... 

現在你可以嘗試結合key+value,看看它看起來像你的願望。