简体   繁体   中英

Angular 2 patchValue in Form Array

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

以下工作:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM