Maniraj Murugan
Maniraj Murugan

Reputation: 9084

Patchvalue in angular form

I my angular application, i am using angular dynamic form in which i am patching value with some json.

  data =
  {
    firstName:"Eliseo",
    lastName:"Plunker",
    myArray:
    [
    {
    emailAddress:"[email protected]",
    brave:"solid"
    },
    {
    emailAddress:"[email protected]",
    brave:"great"
    }
    ]
  }

Patchvalue function:

  fillData()
  {
    this.form = this.qcs.toFormGroup(this.questions);
    for (let i=0;i<this.data.myArray.length;i++)
    {
      this.addControls('myArray');
    }
    //Use patchValue
    this.form.patchValue(this.data);

  }

Here in this function, i want to patch only the myArray values and not the firstName and lastName.

I have tried with this.data.myArray

    this.form.patchValue(this.data.myArray);

But it doesn't work.

The working stackblitz: https://stackblitz.com/edit/angular-x4a5b6-wztvq9

Click over Fill Form button in the above demo to get the patched values in the form..

Kindly help me to patch only the myArray value and leave others empty..

Upvotes: 0

Views: 671

Answers (1)

Bharathkumar kamal
Bharathkumar kamal

Reputation: 211

Try this to patch myArray values

this.form.patchValue({
  'myArray' : this.data.myArray
});

Hope this helps!!

Upvotes: 2

Related Questions