Angular4 - How to set the value of a selection option dynamically

I have a select list with 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 saved as another variable, and I want to set this as the value of the picklist if it matches any.

+5
source share
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> 
+1
source
 <select [(ngModel)]="selectedGroup" (ngModelChange)="onGroupChange($event)"> <option *ngFor="let group of groups" [value]="group.group_name"> {{group.group_name}} </option> </select> 
+1
source

Source: https://habr.com/ru/post/1271952/


All Articles