【问题标题】:Angular 8: Pass Disabled attribute while using addControl Method is not workingAngular 8:使用 addControl 方法时传递禁用属性不起作用
【发布时间】:2021-02-04 20:55:31
【问题描述】:

我正在动态创建表单并将输入控件动态添加到表单中。以下是我的代码:

(<FormGroup>this.formsArr[formName]) = new FormGroup({}); 
this.formsArr[formName].addControl(formControlName, new FormControl('abc', []));

在 HTML 中:

<input
    type="text"
    placeholder="{{ fieldObj.placeholder }}"
    [disabled]="fieldObj.disabledValue"
    [readonly]="fieldObj.readOnlyValue">

如果 readOnlyValue 为真,则上面的输入框设置为只读,但在禁用的情况下,它不起作用并在浏览器中抛出警告以在创建新 FormControl({value: 'abc', disabled: true}) 时设置它

在这里我想传递默认的禁用属性。下面是我的代码:

this.formsArr[formName].addControl(formControlName, new FormControl({value: 'abc', disabled: true}[]));

但是我在输入框中得到默认输入值作为 [Object Object] 并且输入框也没有被禁用。

【问题讨论】:

标签: angular angular-reactive-forms


【解决方案1】:

如果我正确理解你的意思,这里和例子对我有用

this.requestForm.addControl('ticketCauses', new FormControl({ value: [], 
     disabled: true }, Validators.required));

在哪里 ticketCauses 是 formControl 名称 value: [] 是初始值 disabled: true 设置该字段默认禁用 Validators.required 是要求该字段不为空的验证器

【讨论】:

    猜你喜欢
    • 2021-08-28
    • 2022-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-22
    • 2019-01-02
    • 2011-09-12
    相关资源
    最近更新 更多