Home > Back-end >  how to set value to form control array of objects
how to set value to form control array of objects

Time:04-25

In a formgroup we can patch or set value to control name like the ff this.modelForm.get('data1').patchValue('sample data') , but what if the structure is an array of objects below like .

How do I set value or patch value to testData which is inside data1? . Thanks.

  "data1": [
            {
                "testData": "2022-04-01T00:00:00",
       
            }
        ]

.....

CodePudding user response:

Simply as that


myControl.setValue([...myControl.value, 'new value']);

CodePudding user response:

you need to access that array and append to the array, like this.

this.modelForm.get('data1').push('add data here').

this will automatically take the new data and append it in the formControl.

  • Related