Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

182
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda