form.valueChanges doesn't emit values for disabled controls

前端 未结 2 854
鱼传尺愫
鱼传尺愫 2021-01-05 07:03

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

相关标签:
2条回答
  • 2021-01-05 07:29

    The value from a disable input is ignored (try to submit a form with a disabled input: it won't be posted).

    You can change it to 'readonly'

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

    Updated example.

    0 讨论(0)
  • 2021-01-05 07:51

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

    More information in the API documentation

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

    Here is the forked example

    0 讨论(0)
提交回复
热议问题