Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular4 - How to set select option value dynamically

I have a select list with the code -

<select (change)='onGroupChange($event)'>
    <option *ngFor="let group of groups" value={{group.group_name}}>
        {{group.group_name}}
    </option>
</select>

Now I have a group name value saved as a different variable and I want to set that as the select list value if it matches any.

like image 274
Vipul Sharma Avatar asked Sep 19 '17 17:09

Vipul Sharma


2 Answers

<select (change)='onGroupChange($event)'>
    <option *ngFor="let group of groups" value={{group.group_name}} [selected]="group.group_name==myVariable">
        {{group.group_name}}
    </option>
</select>
like image 157
Michael Avatar answered Oct 28 '22 06:10

Michael


<select [(ngModel)]="selectedGroup" (ngModelChange)="onGroupChange($event)">
    <option *ngFor="let group of groups" [value]="group.group_name">
    {{group.group_name}}
    </option>
</select>
like image 24
Hui Shi Avatar answered Oct 28 '22 06:10

Hui Shi