【问题标题】:Disabled attribute not working in angular reactive form [duplicate]禁用的属性不能以角度反应形式工作[重复]
【发布时间】:2019-06-12 22:56:24
【问题描述】:

我有一个角度反应形式的电子邮件字段,如果表单处于编辑模式(与添加模式相反),我希望禁用该字段。我正在使用

disabled: typeof user.user_id === 'string'

如果我在下面的modelToForm 方法的开头设置一个断点并将typeof user.user_id === 'string' 粘贴到控制台中,它会返回true,所以它看起来应该可以工作,但表单中的字段没有被禁用。如果我将其更改为“禁用:true”,则该字段被禁用。有人遇到过这个吗?

    public modelToForm(user: UserModel): FormGroup {
        return this.fb.group({
            firstName: [user.first_name, [
                Validators.required,
                Validators.maxLength(UserFormHelper.maxLengthName),
                UserFormHelper.validateWhitespace
            ]],
            email: [{value: user.email, disabled: typeof user.user_id === 'string'}, [
                Validators.required,
                Validators.maxLength(UserFormHelper.maxLengthEmail),
                Validators.pattern(UserFormHelper.emailPattern)
            ]],
        });
    }

【问题讨论】:

  • 你确定typeof user.user_id === 'string'是你当时想要的条件吗?
  • 它应该只适用于 - 'disabled: user.user_id',但使用 typeof user.user_id === 'string' 也应该有效。每当我在方法开始处设置断点并点击编辑时,条件似乎为真(只要我点击添加,它就为假)

标签: angular typescript angular-reactive-forms


【解决方案1】:

我想我明白了!在表单初始化上设置禁用是静态的?当您的病情发生变化时,您需要手动更改它吗?

control.disable()
control.enable()

【讨论】:

  • 谢谢大家,我得到了它的工作,我从反应形式中删除了它并将其放入 html - [attr.disabled]="user.user_id"。很奇怪,但是当我以反应形式使用它时它曾经工作过
猜你喜欢
  • 1970-01-01
  • 2021-11-30
  • 1970-01-01
  • 2018-08-28
  • 2019-04-21
  • 1970-01-01
  • 1970-01-01
  • 2018-09-24
  • 2021-11-25
相关资源
最近更新 更多