2

I am trying to create a basic form in Angular 2 on plunker but faced with the below error on Chrome Dev's Console:

VM8247:27 EXCEPTION: Error in ./App class App - inline template:1:12 caused by: formGroup expects a FormGroup instance. Please pass one in.

       Example:


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

    In your class:

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

Plus there are some other errors also which point to more or less the above error only. I came across other answers on SO related to this but most of them had typos in their code.

Please have a look at the plunker .

Aakash Thakur
  • 3,837
  • 10
  • 33
  • 64
  • Suggested close reason: _This question was caused by a problem that can no longer be reproduced or a simple typographical error._ – halfer Sep 25 '17 at 21:22

2 Answers2

1

You actually have a typo in your plunker...

Instead of

constructor(fb:FormBuilder){

You write

cosntructor(fb:FormBuilder){

That is why your form isn't getting initialized and remains undefined for Angular.

smnbbrv
  • 23,502
  • 9
  • 78
  • 109
  • 1
    oooh man. Thanx for the slight fix. I don't know how I didn't see that, was more worried bout the errors. Angular should also give these type errors. – Aakash Thakur Feb 08 '17 at 08:44
  • well, on typescript / angular side it is valid. Nobody restricts you in having function with the name `cosntructor` :D – smnbbrv Feb 08 '17 at 08:46
0

Initialize your FormGroup instance empty at the time of declaration :

myFormGroup : FormGroup = new FormGroup({})
Benkerroum Mohamed
  • 1,867
  • 3
  • 13
  • 19