2016-12-03 125 views
1

挣扎了一下从下拉选择的值。获取选定的值下拉角2

因为这是返回值undefined

如何在Angular 2中获得此选定值(opl.Opl_Id)?

<form class="form-inline" novalidate> 
    <select class="form-control" (change)="onChange(opl)"> 
      <option [selected] = "opl.OplDescription == selectedOpl" *ngFor="let opl of existingOpls" [ngValue]="opl.Opl_Id">{{opl.OplDescription}}</option> 
    </select 

//component 
onChange(value) { 
    console.log(value); 
} 

回答

1

你为什么叫额外的更改事件只得到选择的值???使用[(ngModel)]获得更新的值,而不是如下图所示,

<select class="form-control" [(ngModel)]="selectedVal">    //<<<---here 

     <option [attr.selected] = "opl.OplDescription == selectedOpl" //<<<---here 
       *ngFor="let opl of existingOpls" 
       [ngValue]="opl.Opl_Id"> 
       {{opl.OplDescription}} 
     </option> 

</select> 

{{selectedVal}} 
1

我发现这更容易..希望它可以帮助开发人员未来。

<select class="form-control" id="select" (change)="Selected($event.target.value)"> 
     <option *ngFor="let item of items" [value]="item.id">{{item.value}}</option> 
    </select> 


    Selected(value: any) { 

      console.log(value); 

       }