最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angularjs populate select options with json - Stack Overflow

programmeradmin0浏览0评论

I am looking to populate select option with values from a basic json array.

The example I have is a country select. Each country has an id element and a name plus other fields that I can ignore. Basically I would like to say put one value in the value="" field and another between the <option>tags</option>

html snippet

<div ng-controller="DemoCtrl">

  <p>populate select options with ajax</p>

    <select id="Country" name="Country" class="form-control" size="10" 
        ng-model ="chooseCountries">                                
            <option ng:repeat="country in chooseCountries" value="{{country.id}}">     
                {{country.name}}
            </option>
    </select>

</div>

javascript snippet

'use strict';

function DemoSelectCtrl($scope) {

    $scope.chooseCountries=[
        {countryId : 1, name : "France - Mainland", desc: "some description" },
        {countryId : 3, name : "Gibraltar", desc: "some description"},
        {countryId : 3, name : "Malta", desc: "some description"}
    ];  

});

I have put it together here js fiddle.. I think I'm missing something

I am looking to populate select option with values from a basic json array.

The example I have is a country select. Each country has an id element and a name plus other fields that I can ignore. Basically I would like to say put one value in the value="" field and another between the <option>tags</option>

html snippet

<div ng-controller="DemoCtrl">

  <p>populate select options with ajax</p>

    <select id="Country" name="Country" class="form-control" size="10" 
        ng-model ="chooseCountries">                                
            <option ng:repeat="country in chooseCountries" value="{{country.id}}">     
                {{country.name}}
            </option>
    </select>

</div>

javascript snippet

'use strict';

function DemoSelectCtrl($scope) {

    $scope.chooseCountries=[
        {countryId : 1, name : "France - Mainland", desc: "some description" },
        {countryId : 3, name : "Gibraltar", desc: "some description"},
        {countryId : 3, name : "Malta", desc: "some description"}
    ];  

});

I have put it together here js fiddle.. I think I'm missing something

Share Improve this question edited Feb 10, 2015 at 14:23 Muhammad Reda 27k14 gold badges98 silver badges106 bronze badges asked Dec 24, 2014 at 11:29 Robbo_UKRobbo_UK 12.1k28 gold badges90 silver badges120 bronze badges 2
  • 1 change the value of option value="{{country.countryId}}" – vamsikrishnamannem Commented Dec 24, 2014 at 11:36
  • 1 It is better advised to use ng-options. – Wtower Commented Mar 14, 2017 at 17:01
Add a comment  | 

4 Answers 4

Reset to default 3
  1. angular.module('ui.bootstrap.demo', ['ui.bootstrap']) - be sure you have ui.bootstrap. Read how to install it http://angular-ui.github.io/bootstrap/
  2. Here is your updated jsfiddle http://jsfiddle.net/e31k5e6L/1/

In this above example you are missing value attribute change this value="{{country.countryId}}". try this

<select id="Country" name="Country" class="form-control" size="10" ng-model ="chooseCountries">                                
    <option ng:repeat="country in chooseCountries" value="{{country.countryId}}">
        {{country.name}}
    </option>
</select>

and see the example click here

You miss-typed the value attribute, change it to country.countryId.

Also, set ng-model directive to a single countryId value (or array of country IDs) instead of the full object.

<select id="Country" name="Country" ng-model ="selectedValue"> 
    <option ng:repeat="country in chooseCountries" value="{{country.countryId}}">
        ...   

JS:

function DemoSelectCtrl($scope) {

    $scope.selectedValue = 1;    

    $scope.chooseCountries=[
        {countryId : 1, name : "France - Mainland", desc: "some description" },
        {countryId : 3, name : "Gibraltar", desc: "some description"},
        {countryId : 3, name : "Malta", desc: "some description"}
    ];  

});

The best way to populate a standard select(you can use angular-ui-select too) with objects is to use "ng-options" directive and "ng-repeat" using "track by id" (see AngularJS doc), this works well with ng-model. This is an example of how to use it:

<select id="select_field" name="select_field"
   ng-model="vm.selectedCountry"
   ng-options="country as country.name for country in vm.chooseCountries track by country.countryId">
   <option value="">-- Select Country --</option>
</select>

I must say that "vm" is in case of "controllerAs" defined in the controller, if you use the $scope directly, you can remove the "vm".

This is the best way that I found to populate a select field.

发布评论

评论列表(0)

  1. 暂无评论