我正在尝试实现selectAll()和deselectAll(),但是显示错误:
我正在使用模板驱动的表单。这是我的代码:
<mat-selection-list name="role" *ngIf='userActions === AssignmentTypesForProductivityAppsEnum.UserRole'
[(ngModel)]="assignProductivityApplication.roleIds" #UserRoles="ngModel" multiple required>
<mat-list-option #allSelected (click)="selectAll(allSelected.selected)" [value]="0"
checkboxPosition="before">All</mat-list-option>
<mat-list-option *ngFor="let role of userRoles | searchFilter : searchTerm : 'role'; let i=index"
checkboxPosition="before" [value]='role.guId'>
{{role.role}}
</mat-list-option>
</mat-selection-list>
在ts文件中:
@ViewChild('allSelected', {static: true}) private allSelected: MatSelectionList;
selectAll(checkAll) {
if (checkAll) {
this.assignProductivityApplication.UserRoles = [];
this.assignProductivityApplication.UserRoles.push(... this.userRoles.map(item => item.guId));
this.allSelected.selectAll();
}
}
答案 0 :(得分:3)
matSelectionList将导出到mat-selectin-list组件中。将allSelected模板变量移动到mat-selection-list
尝试一下:
<mat-selection-list name="role" #allSelected *ngIf='userActions === AssignmentTypesForProductivityAppsEnum.UserRole'
[(ngModel)]="assignProductivityApplication.roleIds" #UserRoles="ngModel" multiple required>
<mat-list-option (click)="selectAll(allSelected.selected)" [value]="0"
checkboxPosition="before">All</mat-list-option>
<mat-list-option *ngFor="let role of userRoles | searchFilter : searchTerm : 'role'; let i=index"
checkboxPosition="before" [value]='role.guId'>
{{role.role}}
</mat-list-option>
</mat-selection-list>