当我尝试使用$event.target.checked with angular mat-checkbox时,它给出了一个错误,即“Cannot read property 'checked' of undefined at Object.eval [as handleEvent]”这是为Angular 8上的复选框传递多个值
onChange(cls: string, isChecked: boolean) {
const clsFormArray = < FormArray > this.myForm.controls.usercls;
if (isChecked) {
clsFormArray.push(new FormControl(cls));
} else {
let index = clsFormArray.controls.findIndex(x => x.value == cls);
clsFormArray.removeAt(index);
}
}
<mat-checkbox class="example-margin" (change)="onChange(data.cls, $event.target.checked)">
{{ data.cls }}
</mat-checkbox>
<mat-checkbox class="example-margin"(change)="onChange(data.cls,$event.target.checked)">
{{ data.cls }}
</mat-checkbox>
onChange(cls: string, isChecked: boolean) {
const clsFormArray = <FormArray>this.myForm.controls.usercls;
if (isChecked) {
clsFormArray.push(new FormControl(cls));
} else {
let index = clsFormArray.controls.findIndex(x => x.value == cls);
clsFormArray.removeAt(index);
}
}
我期望类被推入并添加到数组中,以便我可以打印它
3条答案
按热度按时间qxsslcnc1#
不需要发送
target
。只需发送$event.checked
我已经更新了下面的代码。zaqlnxep2#
6fe3ivhb3#
使用$event.source.value,即使用source代替target。如果source不起作用,请在浏览器中检查您的Web应用程序并检查对象名称,然后使用该对象名称代替target