更改不适用于选择的反应形式

时间:2018-06-05 12:37:06

标签: angular

我正在开发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");
    }

1 个答案:

答案 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);
    }

}