i have a form with a form array:
this.myForm = this._fb.group({
id: [this.model.id],
customer_id: [this.model.customer_id],
orderlines: this._fb.array([])
});
The form array:
return this._fb.group({
id: [orderline.id],
order_id: [orderline.order_id],
factor: [orderline.factor],
})
Now i want to change the value of the factor field within in the method setFactor(i). The i is the index of the form array orderlines.
setFactor(i) {
this.myForm['orderlines'[i]].patchValue({ factor: 99 }) <--- no error but no change in form
this.myForm.patchValue({ orderlines[i].factor: 99 }) <-- error
}
How can i use patchValue to change a value in a form array?
EDIT
this will give me the value i want to change:
console.log(this.myForm['controls']['orderlines']['controls'][i]['controls']['factor'].value);
'orderlines'[i]
is indexing into the string'orderlines'
, so you'd end up with e.g.this.myForm['o']
. In the second version, presumably the error is that you haven't definedorderlines
anywhere (as far as you've shown) prior to indexing into it. Also that control array is empty, so it's not clear what indexing into it would achieve. Could you give a minimal reproducible example? - jonrsharpe