Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 selected option on condition

I have a select:

  <select id="position">
    <option *ngFor='#contactType of contactTypes' [attr.value]='contactType.contactTypeId'>
      {{contactType.description}}
    </option>
  </select>

I would like to have a selected option on condition: 'contactType.contactTypeId == number' without using ngModel

like image 275
mishap Avatar asked Apr 20 '16 17:04

mishap


1 Answers

I guess this is what you want:

 <select id="position">
    <option *ngFor='#contactType of contactTypes'
      [attr.value]='contactType.contactTypeId' 
      [attr.selected]="contactType.contactTypeId == number ? true : null">
      {{contactType.description}}
    </option>
  </select>

To get the selected attribute removed you need to return null (false results in selected="false").

like image 180
Günter Zöchbauer Avatar answered Oct 19 '22 04:10

Günter Zöchbauer