Original Answer
I'm not entirely sure but I think your formgroups need a name:
myForm = this.fb.group({
fieldOne: [''],
fieldGroup: this.fb.array([
name1: this.fb.group({
groupId: [1],
myValue: []
}),
name2: this.fb.group({
groupId: [2],
myValue: []
}),
name3: this.fb.group({
groupId: [3],
myValue: []
})
])
});
Example html:
<form novalidate
(ngSubmit)="saveProduct()"
[formGroup]="myForm">
<div class="form-group row mb-2">
<label class="col-md-2 col-form-label"
for="fieldOneId">fieldOne</label>
<div class="col-md-8">
<input class="form-control"
id="fieldOneId"
type="text"
formControlName="fieldOne"/>
</div>
</div>
<div formArrayName="fieldGroup">
<div formGroupName="name1">
<div class="form-group row mb-2">
<label class="col-md-2 col-form-label"
for="groupIdId">groupId</label>
<div class="col-md-8">
<input class="form-control"
id="groupIdId"
type="number"
formControlName="groupId"/>
</div>
<!-- Repeat -->
</div>
<!-- Repeat -->
</div>
</div>
</form>
Update
You can actually nest objects in you form object.
You can either use FormsModule and bind to some object initialized in your component.
But I figured you would want to stay with reactive forms. And noticed you can also nest other form objects inside the original one.
item1: Item = new Item();
subForm = this.fb.group({
groupId: [''],
myValue: ['']
})
myForm = this.fb.group({
fieldOne: [''],
fieldGroup: this.fb.array([
this.item1, // <-- With 2-way binding through FormsModule
this.subForm // <-- Nesting other form objects with ReactiveFormsModule
])
});
Here is a Stackblitz with both examples.
Example output:
{
"fieldOne":"bleh",
"fieldGroup":[
{"groupId":1,"myValue":2},
{"groupId":3,"myValue":4}
]
}