form.valueChanges 不会为禁用的控件发出值 [英] form.valueChanges doesn't emit values for disabled controls

查看:25
本文介绍了form.valueChanges 不会为禁用的控件发出值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Angular Reactive 表单.我订阅它的值更改并将更改发送到父组件.某些控件可能会被用户禁用.问题是在发出表单 valueChanges 时,来自禁用控件的值丢失了.我已经设置了一个基本的示例.

I have an Angular Reactive form. I subscribe to its value changes and will emit changes to parent component. Some of the controls might get disabled by the user. The problem is that values from disabled controls are missing when form valueChanges are emitted. I've set a basic example.

选中复选框并禁用电子邮件输入时,不会记录表单控件值.但我想获取所有表单值.

When the checkbox is checked and the email input is disabled, there is no form control value logged. But I'd like to get ALL form values.

推荐答案

使用 FormGroup 的 getRawValue() 来包含控件值,而不管启用/禁用状态.

Use the FormGroup's getRawValue() to include control values regardless of enable/disable state.

API 文档中的更多信息

this.myForm.valueChanges.subscribe(() => {
    this.formValues =  JSON.stringify(this.myForm.getRawValue());
});

这里是分叉的例子

这篇关于form.valueChanges 不会为禁用的控件发出值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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