使用标签和值数组将 select 与 ng-options 绑定

Binding select with ng-options using an array of labels and values

我这辈子都不知道如何使用数组

设置select的labal和值

我有一系列国家

$scope.countries =  [
    {abbr:"US", name:"United States"},
    {abbr:"CA", name:"Canada"},......
]

我希望select这样生成

<select>
  <option value="US">United States</option>
  <option value="CA">Canada</option>
</select>

然而我能达到的最接近的是

<select>
  <option value="1">United States</option>
  <option value="2">Canada</option>
</select>

我使用

实现了这一点
<select class="form-control" ng-options="country.Name for country in countries" ng-model="selectedCountry">

如何使用 ng-options 分配标签和值?

未经测试我认为它只是

ng-options="country.abbr as country.name for country in countries"

对于精确的结构,您需要通过 <option><option> 执行 ng-repeat ng-options 永远不会设置您想要的值,它将始终设置 0,1,2, 3等

HTML

<select ng-model="selectedCountry">
    <option ng-repeat="country in countries" value="{{country.abbr}}" ng-bind="country.name"></option>
</select>

JSFiddle

希望对您有所帮助,谢谢。