Search code examples
angularangular-reactive-formsform-control

Assigning form-control a default from returned backend data


I'm having trouble assigning my form-control default value to a number I'm getting from the backend.

I've saved the number into it's own object and am trying to call for it in the form control.

totalInvoice: number;

finalizeInvoiceCost = this.fb.group({
   totalInvoiceCost: [this.totalInvoice]
});

I expect the form-control to default to the number stored inside totalInvoice but when i log it to the console it shows a value of 'null'


Solution

  • Ok, after you initialize totalInvoice you can set its value in the form like:

    this.finalizeInvoiceCost.patchValue({totalInvoiceCost: this.totalInvoice}); 
    

    You can also use setValue but for that you must give a value to every field in your form or it will crash.