Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angularjs populate select options with json

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

like image 366
Robbo_UK Avatar asked Dec 24 '14 11:12

Robbo_UK


2 Answers

  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/
like image 172
user2700840 Avatar answered Oct 07 '22 10:10

user2700840


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

like image 39
vamsikrishnamannem Avatar answered Oct 07 '22 08:10

vamsikrishnamannem