垫选择列表selectAll()不起作用

时间:2020-01-23 07:52:59

标签: javascript angular typescript angular-material angular8

我正在尝试实现selectAll()和deselectAll(),但是显示错误: enter image description here

我正在使用模板驱动的表单。这是我的代码:

<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();
        }
      }

1 个答案:

答案 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>

Example