I have a nested JSON response coming back from an API and is structured differently then how I need to display it on the template, for example:
@Component({
selector: 'reactive-form-example',
styles: ['./reactive-form-example.component.css'],
template: `
<form [formGroup]="form" (ngSubmit)="onSubmit()">
<div formGroupName="first">
<input type="text" placeholder="some id" formControlName="someId">
<div formGroupName="second">
<input type="text" placeholder="some text" formControlName="someText">
</div>
</div>
</form>
`
})
export class ReactiveFormExampleComponent {
form = new FormGroup({
first: new FormGroup({
someId: new FormControl('587824')
}),
second: new FormGroup({
someText: new FormControl('the sky is blue')
})
});
onSubmit(value) {
console.log('Submit', this.form.value);
}
}
Question: Is it possible to have a formGroupName
nested within another formGroupName
, or is there a better way to achieve this same result using reactive forms?
Angular forms FormGroupName Directive Last Updated : 04 Jul, 2021 In this article, we are going to see what is FormGroupName in Angular 10 and how to use it. The FormGroupName is used to sync a nested FormGroup to a DOM element.
Yes. formGroupName can be nested within another formGroupName. formGroupName and formControlName attributes work by finding a control with that particular name within the parent FormGroup. Note that your problem is caused because you're trying to find a FormGroup named second within the first FormGroup:
What we do with reactive forms instead, is keeping the data in the form model, until the user hits the submit button. Only then the data is copied over to the original model, replacing everything. This type of overwriting everything is called "immutable objects". To create this form-model, angular uses a class called FormGroup.
To create this form-model, angular uses a class called FormGroup. To define our form-model, we create a new FormGroup. The constructor of this class then takes an object, that can contain sub-form-groups and FormControls.
Yes. formGroupName
can be nested within another formGroupName
.
formGroupName
and formControlName
attributes work by finding a control with that particular name within the parent FormGroup
.
Note that your problem is caused because you're trying to find a FormGroup
named second
within the first
FormGroup:
<form [formGroup]="form">
<div formGroupName="first">
<div formGroupName="second">
</div>
</div>
</form>
To make that work, you'd have to adapt your model as follows, where second
becomes a child of first
:
form = new FormGroup({
first: new FormGroup({
someId: new FormControl('587824'),
second: new FormGroup({
someText: new FormControl('the sky is blue')
})
}),
});
The reason why the suggestion of emostafa works, is because you ask the form
instance to get a direct child named second
within the model. Which does exist in this case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With