vkstack
vkstack

Reputation: 1654

Papulate Dropdown options on select using AngularJS

How can i populate options dynamically in select drop-down on click. i want to fetch some data from backend on clicking select box.

`ng-options="type.shorthand as type.name for type in allTypes"`

i want to store value in allTypes when i click on select box to list dropdown.

Upvotes: 0

Views: 107

Answers (2)

Minato
Minato

Reputation: 4533

its generally a good idea to fetch the values for drop-down before hand

<select ng-model="yourFormModel.yourAttributeName" 
        ng-options="type.shorthand as type.name for type in allTypes" 
        ng-init="fetchTypes()"></select>

Somewhere in your controller code.

$scope.fetchTypes = function(){
   $http.get('/path/to/types')
        .then(function(data){
             $scope.allTypes = data;
         });
}

Upvotes: 2

Dhana
Dhana

Reputation: 704

in controller

$scope.dropDownVals = ""; getDataForDropDown();

just on the load of the page you can call a factory/service function from the controller which makes the http request to the server side code to get the json object response and then assign it

in services/factory function http get call to the server side code .then(function(data){ $scope.dropDownVals = data.requiredField;

});

Thank you

Upvotes: 0

Related Questions