首页 > 解决方案 > 如何使用 mat-slide-toggle 禁用角度输入字段?

问题描述

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

标签: angularjshtmltypescript

解决方案


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

    <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 演示显示在更改垫子切换时禁用/启用输入


推荐阅读