mishap
mishap

Reputation: 8505

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

Upvotes: 15

Views: 24311

Answers (1)

G&#252;nter Z&#246;chbauer
G&#252;nter Z&#246;chbauer

Reputation: 657058

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").

Upvotes: 35

Related Questions