Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS ng-options default selected option [duplicate]

How can you set a default option with ng-options?

I've set this in the controller (using the controller as syntax):

this.myOption = this.myOptions[0];

However, that's still returning a blank default option:

<option value="?" selected="selected"></option>

How can I set the default option to the first value in my model ("Item 1")?

Here's a JSBin demo.

like image 573
Ryan Avatar asked Jun 05 '14 14:06

Ryan


2 Answers

either change:

ng-options="value.id as value.label ...

to:

ng-options="value as value.label ...

demo: http://jsbin.com/gewebeqo/4/


or use:

this.myOption = this.myOptions[0].id;

in the controller.

demo: http://jsbin.com/gewebeqo/3/

like image 159
Yoshi Avatar answered Sep 27 '22 21:09

Yoshi


ng-model needs the id of the option you want to select. Therefore you need to use this.myOption = this.myOptions[0].id; instead of this.myOption = this.myOptions[0].

like image 30
Daniel Avatar answered Sep 27 '22 22:09

Daniel