我正在开发Angular2项目,我需要在select inside inside form中设置(更改)事件,但它不起作用。我该怎么办?
这里是代码
<div class="form-group">
<select class="form-control" (change)="changeClassesLevel()" formControlName="ClassesLevel" >
<option *ngFor="let level of classesLevels" [value]="level.Id">{{ level.Level }}</option>
</select>
</div>
在Angular中:
changeClassesLevel(){
console.log("Hit");
}
答案 0 :(得分:1)
我测试了它。工作正常。
<强> component.html 强>
<form [formGroup]="classesLevelsForm">
<div class="form-group">
<select class="form-control" (change)="changeClassesLevel($event.target.value)" formControlName="ClassesLevel">
<option *ngFor="let level of classesLevels" [value]="level.Id">{{ level.Level }}</option>
</select>
</div>
</form>
<强> component.ts 强>
import { FormControl, FormGroup, Validators } from '@angular/forms';
export class Component {
classesLevels: any = [];
classesLevelsForm: FormGroup;
constructor() {
this.classesLevelsForm = new FormGroup({
ClassesLevel: new FormControl(null)
});
this.classesLevels = [{ Id: 1, Level: 10 },{ Id: 2, Level: 2 } ];
}
changeClassesLevel(id: number) {
console.log("Hit");
console.log(id);
}
}