How to set default option in Angular select element

时间:2015-07-28 22:25:26

标签: javascript angularjs

I'm trying to set the default option in a select element in an Angular app. Below is what I've tried, but my select box is empty:

<select ng-init="selectedMax = game.player.max_points"
        ng-options="option for option in game.maxOptions"
        ng-model="selectedMax"
        ng-change="changeMax()"></select>

Basically I want my default option to be game.player.max_points which is a value set by my controller on initiation.

2 个答案:

答案 0 :(得分:0)

Select one of the available options:

<select ng-init="selectedMax = game.maxOptions[0]" ng-options="option for option in game.maxOptions" ng-model="selectedMax" ng-change="changeMax()"></select>

or try setting it in your controller: http://codepen.io/anon/pen/PqxmvG

答案 1 :(得分:0)

I think you have to tell to the ng-options directive what field to use from the options array. Something like this:

<select ng-init="selectedMax = game.player.max_points" 
        ng-options="option.fieldName for option in game.maxOptions"
        ng-model="selectedMax" 
        ng-change="changeMax()">
</select>