在Angular4中的select-option下获取select-selectedIndex和click事件

时间:2017-08-23 09:30:58

标签: angular html-select

我想获得selectedIndex然后获取选择数据模型,然后我发现我可以直接获取所选数据模型Angular2 RC2 Select Option selectedIndex。但是,选项下的单击事件不起作用。

<select class="form-control selectpicker" #Code name="Code" [(ngModel)]="model.Code">
    <option value="" selected="selected">--Please select--</option>
    <option *ngFor="let x of Codes" [value]="x.Code" [selected]="x.Code == model.Code" (click)="searchWithCode(x)">{{x.Name}}</option>
</select>`

searchWithCode(code:any) : void {
    this.model.Name1 = code.Name1;
    this.model.Name2 = code.Name2;
}

1 个答案:

答案 0 :(得分:0)

尝试在下拉列表中使用更改事件:

<select class="form-control selectpicker" #Code name="Code" [(ngModel)]="model.Code" (change)="searchWithCode($event)">
    <option value="" selected="selected">--Please select--</option>
    <option *ngFor="let x of Codes" [value]="x.Code" [selected]="x.Code == model.Code" >{{x.Name}}</option>
</select>