AngularJS - ng-options选择值

时间:2015-12-18 16:12:46

标签: javascript angularjs ng-options

我有一个像这样的数组

[
{"id":"+7940","name":"Abkhazia"},
{"id":"+93","name":"Afghanistan"},
{"id":"+355","name":"Albania"},
{"id":"+213","name":"Algeria"}
]

等...

我的选择看起来像这样

<select ng-model='person.contactCountryCode' 
        ng-options='c.id as c.name + " " + c.id for c in countryCodes'>
        <option value=""> - Code -</option> 
</select>

我要做的是将ID显示为选定的值而不是名称。

我已经尝试过使用ng-options,但我似乎无法得到它。

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

HTML

<div ng-app="myapp">
    <fieldset ng-controller="FirstCtrl">
        <select ng-model="selected" 
          ng-options="c.id for c in person">
        </select>
        <span>{{selected.name}}</span>
    </fieldset>
</div>

控制器

var myapp = angular.module('myapp', []);
    myapp.controller('FirstCtrl', function ($scope) {  
    $scope.person = [
      {"id":"+7940","name":"Abkhazia"},
      {"id":"+93","name":"Afghanistan"},
      {"id":"+355","name":"Albania"},
      {"id":"+213","name":"Algeria"}
    ];
});

选择Id时,将显示相应的名称。

这是一个小提琴:http://jsfiddle.net/KN9xx/814/