Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select first option in select?

Tags:

angular

I'm using this and i added selected in first option but its not working. Any suggestion?

<select class="select-dropdown" [(ngModel)]="selectedTickeDecisionType" (ngModelChange)="onChange($event)">
    <option value="" selected>Odaberi tip odluke</option>
    <option *ngFor="let decisionType of lovData.tticketdecisiontype" value="{{decisionType.code}}">{{decisionType.name}}</option>
</select>   
like image 377
None Avatar asked Dec 24 '22 18:12

None


2 Answers

When selectedTickeDecisionType is initially null

[ngValue]="null"

on the first <option> should do what you want:

<select class="select-dropdown" [(ngModel)]="selectedTickeDecisionType" (ngModelChange)="onChange($event)">
    <option [ngValue]="null" selected>Odaberi tip odluke</option>
    <option *ngFor="let decisionType of lovData.tticketdecisiontype" value="{{decisionType.code}}">{{decisionType.name}}</option>
</select>   

You might need [ngValue]="decisionType.code" on the other options to make this work properly (I haven't tried mixing value and ngValue myself yet).

like image 109
Günter Zöchbauer Avatar answered Dec 28 '22 09:12

Günter Zöchbauer


<option
 *ngFor="let decisionType of lovData.tticketdecisiontype; let i = index"
 [ngValue]="decisionType.code"
 [attr.selected]='i>0 ? "selected" : null'>
  {{decisionType.name}}
</option>
like image 21
Milad Avatar answered Dec 28 '22 11:12

Milad