form.valueChanges 不会为禁用的控件发出值 [英] form.valueChanges doesn't emit values for disabled controls
问题描述
我有一个 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.
this.myForm.valueChanges.subscribe(() => {
this.formValues = JSON.stringify(this.myForm.getRawValue());
});
这篇关于form.valueChanges 不会为禁用的控件发出值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!