Angular 2禁用的控件未包含在form.value中 [英] Angular 2 disabled controls do not get included in the form.value

查看:63
本文介绍了Angular 2禁用的控件未包含在form.value中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到,如果我禁用Angular 2反应形式上的控件,则该控件不会包含在form.value中.例如,如果我如下定义表格:

I have noticed that if I disable a control on an Angular 2 reactive form then the control does not get included in the form.value. For example, if I define my form like below:

this.notelinkingForm = new FormGroup({
    Enabled: new FormControl(settings.Enabled, Validators.required),
    LinkToPreceeding: new FormControl({value: settings.LinkToPreceeding, disabled: !settings.Enabled}, Validators.required),
    LinkingTolerance: new FormControl({value: settings.LinkingTolerance, disabled: !settings.Enabled}, Validators.required)
});

并检查this.notelinkingForm.value,如果启用了所有控件,则输出为:

and check the this.notelinkingForm.value, if all the controls are enabled then output would be:

{"Enabled":true, "LinkToPreceeding": true, LinkingTolerance:"100"} 

但是,当某些控件被禁用时,它将是:

However, when some of the controls are disabled it would be:

{"Enabled":true} 

请注意如何将禁用的控件排除在外.

Notice how the disabled controls are excluded.

我的目的是,当表单更改时,我希望能够将form.value及其所有属性传递给我的其余API.如果其中不包含禁用的项目,则显然不可能.

My intent is that when the form changes I want to be able to pass the form.value with all the properties in it off to my rest API. This won't be possible obviously if it does not contain the disabled items.

我在这里缺少什么吗?这是预期的行为吗?有没有办法告诉Angular将禁用的项目包括在form.value中?

Am I missing something here or is this the expected behavior? Is there a way to tell Angular to include the disabled items in the form.value?

欢迎您提出想法.

推荐答案

您可以使用:

this.notelinkingForm.getRawValue()

来自文档:

如果无论禁用状态如何都希望包括所有值,请使用此方法.否则,value属性是获取组值的最佳方法.

If you'd like to include all values regardless of disabled status, use this method. Otherwise, the value property is the best way to get the value of the group.

这篇关于Angular 2禁用的控件未包含在form.value中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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