Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Cannot read properties of undefined (reading 'checked') Angular Checkbox

I need your help. I try, with the help of Angular, to check my checkbox whether it is pressed or not. If it is pressed, then I want to store the information in the database, and if not pressed, then do nothing. This code, which is shown, unfortunately gives me the following error:

Cannot read properties of undefined (reading 'checked')

What am I doing wrong? Thank you very much)

html

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<mat-checkbox (change)="saveMessage($event)" formControlName="checkboxInviteMessage">
   <mat-label>Save message</mat-label>
</mat-checkbox>

ts

saveMessage(event: any) {
   if (event.target.checked) {
    console.log('added');
    // Add data to database
   } else if (!event.target.checked) {
    console.log('unchecked');
    // Don't add data to database
   }
}

>Solution :

The event parameter on your saveMessage is of type MatCheckboxChange, you don’t have a .target in there.

For more information, I recommend reading the Angular Material docs: link

saveMessage(event: MatCheckboxChange) {
   if (event.checked) {
    // is checked
   } else {
    //.....
   }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading