I'm initializing my form this way, but when I need to edit it, it does not accept the values
component.ts
ngOnInit() {
this.tinvoiceService.initForm();
}
tinvoice.service.ts
form: FormGroup;
constructor(
private _fb: FormBuilder,
private invoiceService: InvoiceService
) {}
initForm(): void {
this.form = this._fb.group({
customer: [null, Validators.required],
totalPrice: 0,
purchases: this._fb.array([])
});
// initialize stream
const myFormValueChanges$ = this.form.controls['purchases'].valueChanges;
myFormValueChanges$.subscribe(purchases => this.updatePurchasesAmount(purchases));
}
from the HTML I pass the values
tinvoice.component.html
<a
class="btn btn-outline-info text-info"
(click)="tinvoiceService.populateForm(invoice)">
Edit
</a>
tinvoice-list.component.ts
populateForm(invoice) {
this.form.setValue(invoice);
}
by console I have this result
any ideas?
Old Question but will give my view
When a user clicks on the edit button, the console logs works indicates
So we start from here and identify the issue in the line
If we log the value of
invoice
at this point we seeIf we log value of
this.form.value
we aeeSo you are trying to assign the form an incorrect structure of the object.
To check that your code is actually working you can simply change
setValue
topatchValue
. Below solutions works aspatchValue
will only look for form controls that only exists unlikesetValue
. To usesetValue
on the other hand you will have to ensure that all the properties in your object are represented in the FormGroup, otherwise remove those properties from the objectBelow is a Demo showing a solution by changing
setValue
topatchValue