Search code examples
angularformsangular-reactive-formsformarray

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?


Solution

  • 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