3

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?

3
  • Can you create stackblitz? Commented Aug 12, 2019 at 15:49
  • @Chellappan here's the sample stackblitz.com/edit/angular-c6xqxm Commented Aug 12, 2019 at 16:46
  • related post: related post: stackoverflow.com/a/77514234/6908282 Commented Jan 17 at 5:10

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

d*** you're so good. This is what I'm looking for. Spent 7 hours on this one
0

I took a look at you StackBlitz example here https://stackblitz.com/edit/angular-c6xqxm and I think I found the issue. You have to nest the [formGroupName]="1" in a child element of the *ngFor

Instead of

<div *ngFor="let group of itemsArray.controls; let i = index;" [formGroupName]="i"> 

    //your code...      

</div>

try

<div *ngFor="let group of itemsArray.controls; let i = index;" >
  <div [formGroupName]="i"> <----------

    //your code...

  </div>
</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.