本文介绍了如何禁用文本区域或 mat-form-field的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的表单字段使用 Angular (4) + Angular Material 和 Reactive Forms.我怎样才能禁用它?我试图在谷歌上搜索 disabled="true" 之类的东西.你能告诉我正确的语法吗?那一定很容易.谢谢!

I am using Angular (4) + Angular Material and Reactive Forms for my Form Field. How can I disable that? I tried to google for things like disabled="true" or something like that. May can you show me the right syntax? That must be easy. Thanks!

我的例子:

<mat-form-field class="xxx-form-full-with">
    <textarea matInput placeholder="My description" formControlName="xxx"></textarea>
</mat-form-field>

推荐答案

使用反应式表单 [disabled] 将不起作用.您需要在 formControl 上设置禁用状态:

With reactive forms [disabled] will not work.You need to set the disabled status on the formControl instead:

this.myForm = this.formBuilder.group({
  xxx: [{value: 'someValue', disabled:true}]
})

还请记住,在执行此操作时,此字段将不会包含在表单对象中,例如在提交时.如果要检查所有值(包括禁用的值),请使用:

Also remember when doing this, this field will not be included in the form object e.g when submitting. If you want to inspect all values, disabled included, use:

this.myForm.getRawValue();

这篇关于如何禁用文本区域或 mat-form-field的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 20:18