Austin Hunter Austin Hunter - 3 months ago 13
AngularJS Question

Populating a Select Box with Nodejs Information

So I have a mongodb database that will house several department names. I want to http.get() the names from my nodejs server. From that get, I want to populate the names into a select box. The important part of this question isn't the node/mongo part, its the js/html part of, I have the response from the get, how do I put it into a select box? Here is my code:



$http.get('http://localhost:8000/getData').then(function(response) {

});

<select placeholder="Select a User">
<option ng-model="selectUser"></option>
</select>




Answer

First, you have to put the data from the response object into a controller property like this:

$http.get('http://localhost:8000/getData').then(function(response) {
    $scope.options = response.data;
}); 

And then you can use the ng-options directive from angular to populate the select box with values like this:

<select placeholder="Select a User" ng-model="selectUser" data-ng-options="option.Member for option in options">
 </select>