Reputation: 9
This is the HTML component where checkbox is
<mat-checkbox class="dashboard-content-custom-select-option" id="dashboard-all-checkbox" #allSelector [indeterminate]="someCheckboxesActive()" [checked]="allCheckboxesActive()" (click)="toggleAllSelection($event)">Alle </mat-checkbox>
export class DashboardContentComponent implements OnInit, AfterViewInit, OnDestroy {
@ViewChild('dashboard-all-checkbox') allSelect: MatCheckbox;
toggleAllSelection(event) { // toggle checkbox is controlled from here
console.log(this.allSelect);
if ( event.currentTarget.id === 'dashboard-all-checkbox' && this.selectedValues.length === 6) {
this.dashboardContentForm.get('dashboardContentValue').setValue([]);
this.allSelect.checked = false; // unchecks the checkbox
} else if ( (this.selectedValues.length < 6 && event.currentTarget.id === 'dashboard-all-checkbox') ) {
this.dashboardContentForm.get('dashboardContentValue').setValue(
[ ...this.displayDashboardContentValues.map((dv) => dv.key), ...[0]]
);
this.allSelect.toggle(); //checks the checkbox
}
}
}
To be precise, everything works fine. However, the only problem is that I get TypeErrors although the checkbox does whatevery it is supposed to do. If checkbox's id is used, everything is fine(which is the only way that works here in my case). If I use the #allSelector, it is different. How do I prevent these errors from occuring? Besides, you can ignore the grey highlighting in the dropdown, it isn't relevant.
if the select all checkbox is clicked and checked
if the select all checkbox is clicked and unchecked
Upvotes: 0
Views: 323
Reputation: 136174
ViewChild
should be pointing to #allSelector
(local template variable) and not id="dashboard-all-checkbox"
(id selector).
By changing the ViewChild
to lookup allSelector
should fill value.
@ViewChild('allSelector') allSelect: MatCheckbox;
Upvotes: 1