0

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.

2
  • You can use ng-click inside ng-click call one function to fetch data from backend and push the same data to allTypes. make sure allTypes should be initialized as empty array. Commented Jan 6, 2016 at 11:24
  • This question is answered in this comment: stackoverflow.com/a/14386972/2115672 Commented Jan 6, 2016 at 11:25

2 Answers 2

2

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;
         });
}
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.