Manually Set Value for FormBuilder Control

前端 未结 11 1095
执笔经年
执笔经年 2020-11-27 11:25

This is driving me nuts, I\'m under the gun and can\'t afford to spend another whole day on this.

I am trying to manually set a control value (\'dept\') within the c

11条回答
  •  日久生厌
    2020-11-27 11:40

    Tip: If you're using setValue but not providing every property on the form you'll get an error:

    Must supply a value for form control with name: 'stateOrProvince'.

    So you may be tempted to use patchValue, but this can be dangerous if you're trying to update a whole form. I have an address that may not have stateOrProvince or stateCd depending upon whether it is US or worldwide.

    Instead you can update like this - which will use the nulls as defaults:

    this.form.setValue( { stateOrProvince: null, stateCd: null, ...address } );
    

提交回复
热议问题