javascript
  • angularjs
  • ng-options
  • 2015-12-18 178 views 0 likes 
    0

    我有像這樣AngularJS - NG選項選擇值

    [ 
    {"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選項,但我似乎無法得到它。

    任何幫助將不勝感激!

    +1

    嘗試'NG選項='c.id作爲c.id在countryCodes''℃。 –

    +0

    http://jsfiddle.net/KN9xx/811/ –

    +0

    @Moshe我只是想要顯示的數字!所以如果選擇「Abkhazia」,它應該顯示「+7940」 – user2085143

    回答

    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/

    相關問題