取消选中mat-slide toggle可以使用两种方法:
示例代码:
<mat-slide-toggle [(ngModel)]="toggle1Value">Toggle 1</mat-slide-toggle>
<mat-slide-toggle (change)="toggle2Change($event)">Toggle 2</mat-slide-toggle>
toggle1Value: boolean = true;
toggle2Change(event: MatSlideToggleChange) {
if (event.checked) {
this.toggle1Value = false; // 取消选中Toggle 1
}
}
示例代码:
<mat-slide-toggle (change)="toggle1Change($event)">Toggle 1</mat-slide-toggle>
<mat-slide-toggle #toggle2 (change)="toggle2Change($event)">Toggle 2</mat-slide-toggle>
import { MatSlideToggle } from '@angular/material/slide-toggle';
import { ViewChild } from '@angular/core';
@ViewChild('toggle2') toggle2: MatSlideToggle;
toggle1Change(event: MatSlideToggleChange) {
if (event.checked) {
this.toggle2.toggle(); // 取消选中Toggle 2
}
}
toggle2Change(event: MatSlideToggleChange) {
// do something
}
以上是取消选中mat-slide-toggle的两种方法。希望对你有帮助!
领取专属 10元无门槛券
手把手带您无忧上云