【发布时间】:2021-05-21 04:36:53
【问题描述】:
我正在尝试全选以进行角度多项选择,它适用于一个选择,但是当您有超过 1 个选择时,我怎样才能使其工作?当我为第二个输入添加全选时,它会在第一个输入上全选
campaign.ts
@ViewChild('mySel') skillSel: MatSelect;
toggleAllSelection() {
this.allSelected = !this.allSelected;
if (this.allSelected) {
this.skillSel.options.forEach( (item : MatOption) => item.select());
} else {
this.skillSel.options.forEach( (item : MatOption) => {item.deselect()});
}
}
campaign.html
<mat-select #mySel multiple formControlName="Branch">
<mat-option [value]="0" (click)="toggleAllSelection()">All items</mat-option>
<mat-option *ngFor="let item of centers" [value]="item.centerCode">{{item.address}}</mat-option>
</mat-select>
<mat-select #mySel multiple formControlName="categoryDescriptions">
<mat-option [value]="0" (click)="toggleAllSelection()">All items</mat-option>
<mat-option *ngFor="let item of categories" [value]="item.description">{{item.description}}</mat-option>
</mat-select>
【问题讨论】:
-
对于 mat-select 全选和取消全选尝试this 解决方案。
-
嘿,谢谢你的回答我也试过了,但我有同样的问题,当你有多个选择时,我怎么能做到这一点?
-
stackoverflow.com/q/66163651/7821499 我也为该答案打开了一个问题
标签: angular typescript