I am using the mat-checkbox component from Angular Material. But when I check on the checkbox, if I reference event.target.querySelector('input[type=checkbox"]').checked
, it will tell me the value is false, and when I uncheck the checkbox, it will tell me the value is true. It should be the reverse.
Also, if I try to use ViewChild
with mat-checkbox, it yields undefined. Therefore, I cannot reach the element using this.el.nativeElement
.
I illustrate the issue with this isolated git repo:
Here is some relevant code from the repo:
// app.component.ts
@ViewChild('ref', {static: false}) el: ElementRef;
doSomething(e) {
// this reads false when you check the checkbox and true when you uncheck the check box:
console.log('checkbox checked? ', e.target.querySelector('input[type="checkbox"]').checked);
// I can't even use ViewChild with mat-checkbox. It is undefined:
// 'cannot read property nativeElement of undefined'
console.log('the native element: ', this.el.nativeElement);
}
// app.component.html
<mat-checkbox
(click)="doSomething($event)"
>Bladder</mat-checkbox
>
Why does event.target.querySelector('input[type=checkbox"]').checked
give the wrong value? And why is this.el
undefined?