Angular 2 disabled controls do not get included in the form.value

后端 未结 4 1528
遥遥无期
遥遥无期 2020-12-07 14:41

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 bel

相关标签:
4条回答
  • 2020-12-07 15:18

    Thank you @Sasxa for getting me 80% what I needed.

    For those of you looking for a solution to the same problem but for nested forms I was able to solve by changing my usual

    this.notelinkingForm.get('nestedForm').value
    

    to

    this.notelinkingForm.getRawValue().nestedForm
    
    0 讨论(0)
  • 2020-12-07 15:21

    Another option that I use is:

    this.form.controls['LinkToPreceeding'].value;

    0 讨论(0)
  • 2020-12-07 15:25

    You can use:

    this.notelinkingForm.getRawValue()
    

    From Angular docs:

    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.

    0 讨论(0)
  • 2020-12-07 15:35

    If you use readonly instead of disabled it's still not editable while the data will be included in the form. But that isn't possible in all cases. E.g. it's not available for radio buttons and checkboxes. See MDN web docs. In those cases you have to apply for the other solutions provided here.

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