Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default text in Angular select

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?

like image 279
fanhats Avatar asked Feb 15 '15 01:02

fanhats


People also ask

How do I set the default option in select?

The default value of the select element can be set by using the 'selected' attribute on the required option. This is a boolean attribute. The option that is having the 'selected' attribute will be displayed by default on the dropdown list.

How do I set default selected value in ng options?

Use ng-init to set default value for ng-options . Save this answer.

How do I select a default dropdown in angular 6?

You can do this: <select class='form-control' (change)="ChangingValue($event)" [value]='46'> <option value='47'>47</option> <option value='46'>46</option> <option value='45'>45</option> </select> // Note: You can set the value of select only from options tag.

What is select in angular?

HTML select element with AngularJS data-binding. The select directive is used together with ngModel to provide data-binding between the scope and the <select> control (including setting default values). It also handles dynamic <option> elements, which can be added using the ngRepeat or ngOptions directives.


2 Answers

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>
like image 134
Michael Coleman Avatar answered Oct 17 '22 20:10

Michael Coleman


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

like image 20
Don Avatar answered Oct 17 '22 19:10

Don