1
votes

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);
2
Neither of those attempts makes sense. '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 defined orderlines 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

2 Answers

4
votes

the following worked:

this.myForm['controls']['orderlines']['controls'][i]['controls']['factor'].patchValue(99)
0
votes

First create a method that returns the form array

  GetOrderLinesArray() {
    return this.myForm.get('orderLines') as FormArray;
  }

Then to patch the value:

setFactor(index) {
this.GetOrderLinesArray().controls[index].get('factor').patchValue(99);
}