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

308
Vistas
How to typecast from AbstractControl to FormGroup in template?

I have a component that takes FormGroup as @Input.

parentComponent:

parentGroup = fb.group({
  childGroup: fb.group({
     nestedControl: fb.control([])
  })
})
<app-my-component [formGroup]="myGroup">

Then in the child component I have:

@Input formGroup: FormGroup
<div *ngIf="formGroup.controls.childGroup.controls.nestedControl === true">
   some content
</div>

I get an error

error TS2339: Property 'controls' does not exist on type 'AbstractControl'.

I guess the child control does not know the structure of formGroup input. How do I typecast AbstractControl to FormGroup within the template? I guess I can make a function that casts in the .ts file and returns formGroup, but that seems cumbersome. Any way to do it in the template? any other clever solutions? :)

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

0

In the child component that error actually points to your nested group. Angular does not know that childGroup is a FormGroup. I don't see why you couldn't just send the child the nested FormGroup instead of the whole form. First of all your code becomes shorter in template as you don't need to traverse downwards, secondly, that error gets fixed as you can type that childGroup is a FormGroup :)

parent:

<app-my-component [formGroup]="myGroup.get('childGroup')">

and child:

@Input() formGroup: FormGroup;

<div *ngIf="formGroup.get('nestedControl') === true">
   some content
</div>

Notice I also used get. I prefer that myself :)

EDIT: If you need to pass the parent formgroup, you can access nested propertied just like we use in JS objects, i.e using .:

<div *ngIf="formGroup.get('childGroup.nestedControl').value === true">
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