首页 > 解决方案 > Angular Material 的垫子自动完成输入未按预期禁用

问题描述

我正在尝试禁用 Angular Material Autocomplete 组件。我本来希望能够disabled在输入上进行设置,但这无济于事。(我还尝试设置disabledmat-form-field 和 mat-autocomplete。)设置matAutocompleteDisabled输入阻止了选项显示,但仍然允许在字段中输入。输入设置readonly阻止了输入,但它不会改变 UI,所以看起来这会让用户感到困惑。这是一个错误,还是我错过了什么?

这是我到目前为止最接近的,使用readonly(并且禁用没有按预期工作)

<form class="example-form">
  <mat-form-field class="example-full-width">
    <input type="text"
           disabled readonly
           placeholder="Pick one"
           aria-label="Number"
           matInput
           [formControl]="myControl"
           [matAutocomplete]="auto">
    <mat-autocomplete #auto="matAutocomplete">
      <mat-option *ngFor="let option of options" [value]="option">
        {{option}}
      </mat-option>
    </mat-autocomplete>
  </mat-form-field>
</form>

标签: angular-material

解决方案


您应该使用 formControl 来设置它,例如:

this.formGroupName.controls['myControl'].disable()

推荐阅读