Default text in Angular select
Asked Answered
T

2

7

By default angular uses an empty value if nothing has been selected in a <select>. How can I change this to a text that says Please select one?

Tendon answered 15/2, 2015 at 1:19 Comment(0)
M
18

You didnt provide any code so I can't give an example relative to your code, but try adding an option element, e.g.

<select ng-model="item" ng-options="item.category for item in items"
        ng-change="doSomething()">
    <option value="">Please select one</option>
</select>
Mesothorax answered 15/2, 2015 at 1:29 Comment(1)
To add: Having value="" is important. It won't work if you just omit the value attribute.Compo
Y
2

Via the ng-selected attribute:

<select>
  <option>Hello!</option>
  <option ng-selected="selected">Please select one</option>
  <option>Another option</option>
</select>

Check out the reference: https://docs.angularjs.org/api/ng/directive/ngSelected

Yehudi answered 15/2, 2015 at 1:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.