我试图toggle
在toggle()
通过代码:
模板
<mat-slide-toggle color="primary" (change)="updateFunc($event)"></mat-slide-toggle>
ts
updateFunc(e) {
// do some checks to see if can be activated
// if can't be activated, don't slide to on
e.checked = false; // <--- does not work
e.toggle(); // <-- does not work
}
有任何想法吗?
- 编辑 -
为了明确起见,在更改事件之后,该按钮被切换为打开状态。我的功能正在运行以进行一些测试。如果测试未通过,我想将滑块切换回关闭位置。那么,如何在代码中切换按钮(打开或关闭)?这只是在我的代码中使用toggle()方法或在我的代码中取消选中开关的问题。
https://stackblitz.com/edit/angular-material-edh46h
<mat-slide-toggle #toggleElement class="example-margin" [checked]="checked" (change)="updateFunc($event)">
Slide me!
</mat-slide-toggle>
@ViewChild("toggleElement") ref: ElementRef;
checked: boolean;
ngOnInit() {
this.checked = true;
}
updateFunc(e) {
const someCondition = true;
if (someCondition) {
this.ref.checked = false;
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句