我有一系列国家:
var countriesList: [
{name: "Israel", code: "IL"},
{name: "India", code: "IN"},
{name: "Andorra", code: "AD"}
]
以及一系列选定的国家:
selectedCountries: [
{
country:"IL"
}
]
我使用 select2 来选择国家。我从 ng-repeat 开始生成<options/>
标签:
<select
id="countriesList"
ui-select2
multiple
ng-model='data.selectedCountries'
data-placeholder='Choose or Search for Countries'
name='locations'
ng-change='geoTargetingChanged()'>
<option ng-repeat="country in data.countriesList" value="{{country.code}}">{{country.name}}</option>
</select>
这种方法效果很好,但它导致表单$dirty
在开始时是正确的。所以我开始使用 `ng-options- 机制(在阅读了这个答案之后):
<select
id="countriesList"
ui-select2
multiple
ng-model='data.selectedCountries'
data-placeholder='Choose or Search for Countries'
name='locations'
ng-change='geoTargetingChanged()'
ng-options="country.code as country.name for country in data.campaignSettings.countriesList">
<option></option>
</select>
现在的问题是项目的值不是国家代码,而是它们在数组中的索引。
我错过了什么吗?