AngularJS ng-options default selected option [duplicate]
Asked Answered
H

2

7

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.

Hoop answered 5/6, 2014 at 14:12 Comment(1)
@SergiuParaschiv He is using ng-model.Avicenna
A
12

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/

Avicenna answered 5/6, 2014 at 14:16 Comment(0)
C
3

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].

Circlet answered 5/6, 2014 at 14:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.