While working with Angular mat-checkbox, I encountered an error when trying to use $event.target.checked. The error message stated - "Cannot read property 'checked' of undefined at Object.eval [as handleEvent]". This issue arose while attempting to pass multiple values for a checkbox in Angular8.
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);
}
}
I am hopeful that the classes will be successfully pushed and added to the array so that I can print them.