最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular4 - How to set select option value dynamically - Stack Overflow

programmeradmin1浏览0评论

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.

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.

Share Improve this question asked Sep 19, 2017 at 17:50 Vipul SharmaVipul Sharma 7983 gold badges11 silver badges25 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 9
<select (change)='onGroupChange($event)'>
    <option *ngFor="let group of groups" value={{group.group_name}} [selected]="group.group_name==myVariable">
        {{group.group_name}}
    </option>
</select>
<select [(ngModel)]="selectedGroup" (ngModelChange)="onGroupChange($event)">
    <option *ngFor="let group of groups" [value]="group.group_name">
    {{group.group_name}}
    </option>
</select>
发布评论

评论列表(0)

  1. 暂无评论