Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular PatchValue is not working with FormArray

I am trying to patch a value so my input works like ng model

here's the code

 this.purchaseOrderForm.patchValue({
      itemForm: {
        inputSubTotal: this.tempSellPrice.valueOf().toString()
      }

    });

but it doesn't work

 this.purchaseOrderForm.at(i).patchValue({
      itemForm: {
        inputSubTotal: this.tempSellPrice.valueOf().toString()
      }

    });

doesn't work also, i = index count.

is there a way to update my form without saving it yet?

like image 518
Mark Avatar asked Mar 20 '26 02:03

Mark


1 Answers

unfortunately, you can't patch or set a value directly onto a form array, you need to patch or set the values of the form groups / controls within it, like so:

  onChangeState(i){
    const fg = this.itemsArray.at(i);
    const fgValue = fg.value;
    fg.patchValue({
      total: fgValue.fvalue + fgValue.svalue
    });
  }

here is a blitz demo: https://stackblitz.com/edit/angular-wyly3s?file=src/app/app.component.ts

I cant' say what exactly is wrong in your question code without knowing the true structure of the form you're working with

like image 97
bryan60 Avatar answered Mar 22 '26 22:03

bryan60



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!