将表单状态对象中的“已禁用"传递给FormControl构造函数不起作用 [英] Passing 'disabled' in form state object to FormControl constructor doesn't work

查看:180
本文介绍了将表单状态对象中的“已禁用"传递给FormControl构造函数不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们之前已经看到过:

您似乎正在使用带有反应形式的Disabled属性 指示.如果将禁用设置为true 当您在组件类中设置此控件时,disabled属性实际上将在DOM中设置为 你.我们建议使用这种方法来避免检查后更改"错误.

It looks like you're using the disabled attribute with a reactive form directive. If you set disabled to true when you set up this control in your component class, the disabled attribute will actually be set in the DOM for you. We recommend using this approach to avoid 'changed after checked' errors.

输出警告的示例:

  form = new FormGroup({
    first: new FormControl({value: 'Nancy', disabled: true}, Validators.required),
    last: new FormControl('Drew', Validators.required)
  });

但是当我尝试像它说的那样做时,它是行不通的.因此,我在SO上搜索了这个问题,然后发现了许多类似的问题,但他们都说要呼叫手动.我不想这样做,但是在表单状态对象"中将disabled传递给构造函数是行不通的.为什么不呢?

But when I try to do it like it says, it doesn't work. So I search for the issue on SO, and I find a number of questions like this, but they all say to call FormControl.disable() manually. I'd like to not have do that, but passing disabled in the 'form state object' to the constructor doesn't work. Why not?

这是我的代码:

this.registerForm('someName', {
  firstName: new FormControl({disabled: true}),
});

这不是此问题的重复,因为我在问为什么不这样做? 应该工作,而无需其他函数调用或在模板中添加内容.我从答案中发现的内容很关键,而且可能是对Angular设计的疏忽.

This is not a duplicate of this question, as I was asking why something doesn't work that should, without an additional function call, or adding things to the template. And what I discovered through my answer is pretty key, and possibly an oversight in design of Angular.

推荐答案

实际上,它不起作用的原因是我在表单状态对象中没有值".应该是:

Actually, the reason it didn't work was that I did not have 'value' in my form state object. It should have been:

this.registerForm('someName', {
  firstName: new FormControl({value: '', disabled: true}),
});

这篇关于将表单状态对象中的“已禁用"传递给FormControl构造函数不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆