繁体   English   中英

如何使用 mat-slide-toggle 禁用角度输入字段?

[英]How to disable angular input field by using mat-slide-toggle?

如何在反应形式中使用mat-slide-toggle禁用角度 mat-form-field(input) 字段,当输入字段上的滑动切换为 true 时,应启用它,否则应禁用输入字段。

您可以在mat-slide-toggle上使用change方法作为

    <mat-slide-toggle 
      (change)="changeDisable()" formControlName="enableWifi">
      Enable Wifi</mat-slide-toggle>
<mat-form-field class="demo-full-width">  
        <input [disabled]="checked" formControlName="FirstName" matInput
         placeholder="First Name">  
 </mat-form-field> 

changeDisable() {
  if (this.formGroup.controls['FirstName'].disabled) {
   this.formGroup.controls['FirstName'].enable() ;
  } else {
    this.formGroup.controls['FirstName'].disable() ;
  }

}

Stackblitz 演示显示输入禁用/启用更改垫切换

请试试这个

在 HTML 中:

<mat-slide-toggle (change)="toggle($event)">Slide me!</mat-slide-toggle>
<input [attr.disabled]="show ? '' : null"/>{{show}}

在 ts:

    show: boolean = false;
    toggle(e: any) {
      e.checked? this.show = true: this.show = false;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM