0

I know this has been asked a thousand times, but 99% of the solutions seem to be typos. I have reduced the code to what is actually suggested in the error message:

error

In my html:

<div [formGroup]="myGroup">
  <input formControlName="firstName">
</div>

In my ts

import { Component, OnInit } from '@angular/core';
import { FormControl, FormGroup } from '@angular/forms';

@Component({
  selector: 'app-my-info',
  templateUrl: './my-info.component.html',
  styleUrls: ['./my-info.component.scss']
})
export class MyInfoComponent implements OnInit {

  myGroup: FormGroup;


  ngOnInit() {
    this.myGroup = new FormGroup({
      firstName: new FormControl()
    });
  }

}

I have spent hours and hours on this. The code is working elsewhere in my app. Any help would be greatly appreciated!

4
  • What formsModule have u imported to the featuremodule? Commented Aug 5, 2019 at 15:57
  • I hope you have imported FormsModule in your module which has your component [i.e. app.module.ts]? Commented Aug 5, 2019 at 15:57
  • can you reproduce your issue stackblitz.com/edit/stackoverflow-57362093?file=src/app/… Commented Aug 5, 2019 at 16:04
  • 1
    @Jacksnap13, please chack my comment to the response of Obl. Really I think that the place to create the form is in the ngOnInit Commented Aug 5, 2019 at 16:29

3 Answers 3

2

My guess is that the problem is you are doing this inside ngOnInit(). Read up on lifecycle hooks: https://angular.io/guide/lifecycle-hooks

In the official docs it shows the form being setup just as a variable

export class ProfileEditorComponent {
  profileForm = new FormGroup({
    firstName: new FormControl(''),
    lastName: new FormControl(''),
  });
}
Sign up to request clarification or add additional context in comments.

1 Comment

another option I like more is use in .html <form *ngIf="myGroup" [formGroup]="myGroup">
0

Wow. Thanks for all of your input everyone. It turned out I had another component of the same name buried elsewhere in the application. My ts file was not even being recognized. Angular works in mysterious ways :(

Comments

0

At form tag in your component.html add this: *ngIf="{your form group name}"

for example:

 <form *ngIf="updateBookForm" [formGroup]="updateBookForm" (ngSubmit)="onSubmit()">

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.