简体   繁体   中英

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:"eliseo@plunker.com",
    brave:"solid"
    },
    {
    emailAddress:"eliseo@plunker.org",
    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..

Try this to patch myArray values

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

Hope this helps!!

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