Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Property 'controls' does not exist on type 'AbstractControl'.ngtsc(2339)

enter image description hereGetting this weird issue in my template please take a look if this can be resolved. I tried some answers related to this but i got nothing.

  ngOnInit(): void {
    this.signUpForm = new FormGroup({
      'userName': new FormControl(null, Validators.required),
      'email': new FormControl(null, [Validators.required, Validators.email]),
      'gender': new FormControl('male'),
      'hobbies': new FormArray([])
    })
  }

  onAddHobby() {
    const control = new FormControl(null, Validators.required);
    (<FormArray>this.signUpForm.get('hobbies')).push(control);
  }
 <div formArrayName="hobbies">
    <h4>your hobbies</h4>
    <button class="btn btn-default" (click)="onAddHobby()">add hobby</button>
    <div
      class="form-group"
      *ngFor="
        let hobbyControl of signUpForm.get('hobbies').controls;
        let i = index
      "
    >
      <input type="text" class="form-control" [formControlName]="i" />
    </div>
  </div>

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

This is happening because the get() method returns an AbstractControl, which doesn’t have the controls property.

To solve it you can add a getter in the class to cast the control to a FormArray

get hobbies(): FormArray {
  return this.signUpForm.get('hobbies') as FormArray;
}

And then use the getter in the template instead as:

<div 
  class="form-group"
  *ngFor="
    let hobbyControl of hobbies.controls;
    let i = index
  "
>

Cheers

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading