如何将 mat-form-field 设置为只读?

How can I set mat-form-field to readonly?

提问人:Carli Beeli 提问时间:6/14/2023 更新时间:6/14/2023 访问量:1867

问:

如何在 Angular 中将 mat-form-field 设置为只读,以便用户仍然可以轻松阅读它,但不能编辑它?

材料 垫形场

评论

3赞 Jake Smith 6/14/2023
你试过了什么?您可以将属性放在输入...readonly

答:

1赞 Rok Benko 6/14/2023 #1

正如上面的评论所建议的,只需将 .readonly<input>

观看现场演示

输入概述示例.html

<form class="example-form">
  <mat-form-field class="example-full-width">
    <mat-label>Favorite food</mat-label>
    <input
      matInput
      placeholder="Ex. Pizza"
      value="Sushi"
      [formControl]="foodCtrl"
      readonly
    />
  </mat-form-field>
</form>

input-overview-example.ts

import { Component, OnInit } from '@angular/core';
import { FormControl } from '@angular/forms';

@Component({
  selector: 'input-overview-example',
  styleUrls: ['input-overview-example.css'],
  templateUrl: 'input-overview-example.html',
})
export class InputOverviewExample implements OnInit {
  foodCtrl: FormControl;
  ngOnInit() {
    this.foodCtrl = new FormControl('');
  }
}