How can I choose the appropriate <select>
tag option using JavaScript or AngularJS in the backend?
Hint: I receive data from an API service and populate a form for editing. Assuming the gender is currently set as Male in the database, how can I display this as the selected option in the <select>
tag so that the user can update it to Female, for example.
Below are my code snippets:
$scope.Gender = [
{ GenderID: "Male", name: "Male" },
{ GenderID: "Female", name: "Female" }
];
//Assigning data to select tag. However, this approach did not work for me:
$scope.Gender.name = $scope.users[id].Gender;
$scope.Gender.GenderID = $scope.users[id].Gender;
<select class="form-control" name="Gender" ng-model="GenderID" ng-options="g.GenderID as g.name for g in Gender" required style="width:98px; color:gray">
<option value="" style="">Gender?</option>
</select>
<input type="button" value="SelectMale()">