Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

184
Vistas
Angular: How do I deal with nested FormGroups in the HMLT Template?

I'm very new to Angular and trying to build a form to collect user information.

Let's say I have a user that can have a "friend" associated with them. A simplified version of my FormGroup looks like this:

userInformation = new FormGroup({
   name: new FormControl('', [Validators.required]),
   age: new FormControl('', [Validators.required]),
   friend: new FormGroup({
      name: new FormControl('', [Validators.required]),
      age: new FormControl('', [Validators.required])
   })
})

In my HTML, I have:

<form [formGroup]="userInformation" (ngSubmit)="onSubmit">
    <input type="text" placeholder="full name">
    <input type="text" placeholder="full name">
    <input type="text" placeholder="full name">
    <input type="text" placeholder="full name">
</form>

How do I bind the first two inputs to the userInformation and the second two inputs to the userInformation.friend?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

For nested formgroups we use formgroupName, so all you need to create is a container, where you set the formGroupName and the "friend" form controls inside it:

<form [formGroup]="userInformation">
  <input type="text" formControlName="name" placeholder="name" />
  <input type="text" formControlName="age" placeholder="age" />
  <ng-container formGroupName="friend">
  <!-- ^^^^^^^^^HERE^^^^^^^^^^^^^^^^-->
    <input type="text" formControlName="name" placeholder="name" />
    <input type="text" formControlName="age" placeholder="age" />
  </ng-container>
</form>
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda