form.valueChanges不会为禁用的控件发出值

问题描述 投票:2回答:2

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

选中该复选框并禁用电子邮件输入时,不会记录任何表单控件值。但是我希望获得所有表格值。

angular angular-reactive-forms
2个回答
6
投票

无论启用/禁用状态如何,使用FormGroup的getRawValue()来包含控制值。

More information in the API documentation

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

Here is the forked example


0
投票

禁用输入的值将被忽略(尝试提交具有禁用输入的表单:它不会被发布)。

您可以将其更改为“只读”

<input formControlName="email" [readonly]="cb.checked">
<input #cb type="checkbox" formControlName="toggleEmail">

Updated example

© www.soinside.com 2019 - 2024. All rights reserved.