首页 > 解决方案 > 重置后角度表单保持禁用

问题描述

我有一个下拉设置为禁用。但是,每次我重置表单时customerForm.reset() ,下拉菜单都会启用,而不是禁用。

其硬编码为 disabled below = true 。

每次重置后如何将其保持为禁用状态?

<form [formGroup]="customerForm">
    <mat-form-field>
        <mat-label>Product List</mat-label>
        <mat-select
          formControlName="product"
          [disabled]="true">
             <mat-option>Select</mat-option>
             <mat-option
             *ngFor="let productItem of productList"
             [value]="productItem "
             >
             {{ productItem }}
             </mat-option>
        </mat-select>
    </mat-form-field>        

注意:希望保留表单中的值,尽可能不使用 getRawValue。

标签: angulartypescriptangular-materialangular-reactive-formsangular11

解决方案


在您的clear/reset函数中,您可以添加以下内容:

this.customerForm.controls['product'].disabled();

或者

你可以在你的初始化formGroup

customerForm = this.fb.group({
   product: [{value: '', disabled: true}]
});

并省略[disabled]="true"你的mat-select


推荐阅读