0

I have the following code:

  <select #typ class="input" (change)="changeIndex(typ.value)">
        <option *ngFor="let creation of creationComponent>{{creation.name}}</option>
  </select>

If I start the application the selection has automatically selected the first option. But I don't want that. Why is this and how can I fix it?

Stanislav Kvitash
  • 4,614
  • 18
  • 29

2 Answers2

5

Found a better answer here: https://stackoverflow.com/a/13492609/5039495

We can use selected, disabled and hidden on the default option, this is recognized by safari.

<select>
  <option selected disabled hidden></option>
  <option>1</option>
  <option>2</option>
</select>

Outdated

The trick is setting an empty option with display:none.

<select>
  <option style="display:none"></option>
  <option>1</option>
  <option>2</option>
</select>

Warning: As reported by other users, this solution doesn't work in Safari and iOS Safari

Icycool
  • 7,099
  • 1
  • 25
  • 33
0

This is a common thing... Just add an empty <option> as the first:

<select #typ class="input" (change)="changeIndex(typ.value)">
  <option></option>
  <option *ngFor="let creation of creationComponent>{{creation.name}}</option>
</select>

You cannot have anything unselected for a <select> tag. This should fix it. Quoting from Turnip's comment: A select has to have a selected option. If you don't explicitly set one using the selected attribute the first will be used. For a better version, you can use the following code with selected attribute:

<select #typ class="input" (change)="changeIndex(typ.value)">
  <option selected="selected" value=""></option>
  <option *ngFor="let creation of creationComponent>{{creation.name}}</option>
</select>
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
  • 1
    Of course I thought about this. But I don't want that it is an option to choose "". If there is any other possibility please let me know, otherwise I have to do that but I knew that solution anyway. But thanks for the answer! And no, not the first one is selected generally. It is only, if there is the #typ and the (change)-event. Otherwise nothing is selected. –  Aug 22 '17 at 11:58