【发布时间】:2022-07-21 15:55:07
【问题描述】:
这是复选框所在的 HTML 组件
<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
}
}
}
确切地说,一切正常。 然而,唯一的问题是我得到了 TypeErrors 虽然复选框做了它应该做的任何事情。如果使用复选框的 id,一切都很好(在我的情况下,这是唯一可行的方法)。如果我使用#allSelector,那就不同了。 如何防止发生这些错误? 此外,您可以忽略下拉菜单中的灰色突出显示,它不相关。
【问题讨论】:
标签: angular typescript checkbox error-handling viewchild