1

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);
1
  • 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? Commented Jan 29, 2017 at 16:14

2 Answers 2

4

the following worked:

this.myForm['controls']['orderlines']['controls'][i]['controls']['factor'].patchValue(99)
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, this worked. Is there any other more cleaner way to achieve it.
0

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);
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.