Suny Saju Suny Saju - 4 months ago 10
AngularJS Question

how to assign value to select tag in angularjs

I have a select box. For updating I retrieve data from database. So how can I assign retrieved values to select options?

For example:

$scope.options = [{ name: "Select Gender", id: 0 }, { name: "Male", id: 1 }, { name: "Female", id: 2 }];


These are the options. I want to assign
Male
to the select box. How can I do this?

This is the plunk I created for reference.

Answer

Please look at the Plunker based on your one.

First of all, you have to do:

$scope.selectedOption = $scope.options[1];

Then ng-options directive should look like:

data-ng-options="o.id as o.name for o in options track by o.id"
Comments