2015-09-05 108 views
2

我有一個ng-repeat遍歷國家名稱和國家代碼的對象。我正在使用ng-selected來預選美國(840),它工作正常。但是,當我將ng模型(signup.user [「country_code」])引入到包含要將選擇綁定到的對象的select元素時,ng-select似乎被signup.user [ 「country_code」]屬性,默認爲空。ngModel覆蓋ng select select選項元素

 <select ng-model='signup.user["country_code"]'> 
     <option ng-repeat="country in signup.country" ng-selected='country["country-code"]=="840"' ng-value='{{country["country-code"]}}'> 
      {{country["name"]}} 
     </option> 
     </select> 

所以只是清除以下版本是成功的,但預選也是白搭由於缺乏約束力,上述版本並結合得很好,但NG選擇被覆蓋。

 <select> 
     <option ng-repeat="country in signup.country" ng-selected='country["country-code"]=="840"' ng-value='{{country["country-code"]}}'> 
      {{country["name"]}} 
     </option> 
     </select> 

這是我的控制器的一個片段,但我懷疑這對解決這個問題很有用。

signup.user = {}; 

    countryCodes.success(function(data) { 
    signup.country = data; 
    }); 

回答

1

所以,只需在控制器中設置國家代碼並使用ngModel即可。您還應該使用ngOptions指令,而不是ngRepeat的:

signup.user = {country_code: 840}; 

HTML:

<select ng-model="signup.user.country_code" 
     ng-options="country['country-code'] as country.name for country in signup.country"> 
</select> 
+0

完美。工作過一種享受。謝謝! –

相關問題