在角度反应形式中动态设置为禁用textarea

时间:2020-10-01 08:15:49

标签: angular angular-reactive-forms reactive-forms

我想根据用户角色动态创建禁用表单,我已经将文本区域设置为这样

<textarea [attr.disabled]="access == 'read'" formControlName="answer"name="" nbInput fullWidthplaceholder="Masukkan jawaban disini"></textarea>

在其他输入中,我使用[disabled]="access == 'read'" idk,它不适用于textarea,并且只能与att.disabled一起使用,但是问题是我得到了textarea但即使access不是{ {1}},其他输入是可编辑的。

enter image description here

所以我检查了开发人员工具,read的textarea已设置为false,因此不应禁用它,但为什么会这样?

enter image description here

2 个答案:

答案 0 :(得分:1)

尝试将其设为三元,例如 [attr.disabled]="(access == 'read')? true : null" 这对我有用。

Source

答案 1 :(得分:0)

让我们尝试一下吗?

constructor(private fb: FormBuilder) {
  this.form = fb.group({
    ...
    answer: [{ value: '', disabled: this.access === 'read' ? true : false }]
  });