在angular2访问选择元素值给出错误

问题描述:

我使用角2 RC4 我的观点是如下:在angular2访问选择元素值给出错误

<select class="form-control" (change)="selectTestName=this.value;getChartData()"> 
    <option *ngFor="let st of selectTestNameList" value="{{st.selectTestName}}">{{st.selectTestName}}</option> 
</select> 

更改后它会引发以下错误:

TypeError: Cannot read property 'value' of undefined 
    at DebugAppView._View_GeneralTrnd0._handle_change_20_0 (GeneralTrnd.template.js:367) 
    at eval (eval at <anonymous> (vendor.js:25), <anonymous>:316:24) 
    at eval (eval at <anonymous> (vendor.js:45), <anonymous>:278:36) 
    at eval (eval at <anonymous> (vendor.js:48), <anonymous>:20:93) 
    at ZoneDelegate.invoke (zone.js:323) 
    at Object.onInvoke (eval at <anonymous> (vendor.js:17), <anonymous>:45:41) 
    at ZoneDelegate.invoke (zone.js:322) 
    at Zone.runGuarded (zone.js:230) 
    at NgZoneImpl.runInnerGuarded (eval at <anonymous> (vendor.js:17), <anonymous>:78:78) 
    at NgZone.runGuarded (eval at <anonymous> (vendor.js:13), <anonymous>:228:73) 

试试这样说:

<select class="form-control" #selectbox (change)="selectTestName=selectbox.value;getChartData()"> 
    <option *ngFor="let st of selectTestNameList" value="{{st.selectTestName}}">{{st.selectTestName}}</option> 
</select> 

你必须分配一个名称的选择框。您可以使用该名称来访问该值。

而不是this.value使用$event.target.value

<select class="form-control" (change)="selectTestName=$event.target.value;getChartData()"> 
    <option *ngFor="let st of selectTestNameList" value="{{st.selectTestName}}">{{st.selectTestName}}</option> 
</select>