Jaroslav Klimčík
Jaroslav Klimčík

Reputation: 4838

Angularjs - set the default value for select without ng-options

I have a classic select

<select class="form-control" id="gender">
    <option value="Male">Male</option>
    <option value="Female">Female</option>
</select>

And I would like to set selected value according variable coming from scope {{profile.gender}}. When a variable is Female then also option value will be selected. Problem is that I don't have options with gender, only string so I need match it and select. Is it somehow possible to do it with easy way?

Upvotes: 1

Views: 353

Answers (1)

You dont see the options because you actually dont have a classic select. Angular overwrites the classic select with their own ng-options version of select.

If you want to get a male/female option you can just use a hardcoded list on ng-options:

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app>
    <select ng-model="profile.gender" ng-options="gender for gender in ['male', 'female']">
    </select>
  Gender: {{profile.gender}}

Upvotes: 1

Related Questions