Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

309
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!