Carmen Mitru
Carmen Mitru

Reputation: 153

How to set the selected value of a <select> in Angular 2?

I have made an select with diff options in angular 2

<select [(ngModel)]="LineBusinnes" (ngModelChange)="onChange($event)" id="selLineOfBusiness" class="form-control">
                                 <option  [ngValue]="i.id" *ngFor="let i of lineBusiness" >{{i.name}}</option>
                            </select> 

Where line businnes is an array.I would like to be selected by default the first option.

How can I do that?

Thanks!

Upvotes: 1

Views: 9042

Answers (1)

Sefa
Sefa

Reputation: 8992

You can get index in ngFor and set selected attribute based on it.

<option  [ngValue]="i.id" *ngFor="let i of lineBusiness; let idx = index" [attr.selected]="idx == 1">{{i.name}}</option>

Upvotes: 1

Related Questions