Skip to content

Can't bind to 'formGroup' since it isn't a known property of 'form' #14288

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
JPtenBerge opened this issue Feb 3, 2017 · 41 comments
Closed

Can't bind to 'formGroup' since it isn't a known property of 'form' #14288

JPtenBerge opened this issue Feb 3, 2017 · 41 comments

Comments

@JPtenBerge
Copy link

JPtenBerge commented Feb 3, 2017

I'm submitting a ...

[x] bug report
[ ] feature request
[ ] support request

Current behavior
Simply using the [formGroup] directive when everything has been setup properly results in the error above.

Expected behavior
The formGroup was one of the most basic directives to use when working with Reactive Forms. It tied a form element to the definition of the form (residing in a component). I expect no error. I've used [formGroup] since 2.0.0-RC5 (I think) and the documentation still says it should work like this, but it doesn't.

Minimal reproduction of the problem with instructions

  1. Using @angular/cli, create a new project.
  2. In app.module.ts, change FormsModule to ReactiveFormsModule (two places).
  3. In app.component,html, add this form HTML:
    <form [formGroup]="myForm">
      Naam: <input type="text" formControlName="name"><br>
      Age: <input type="number" formControlName="age">
    
      <button>Save</button>
    </form>
    
  4. app.component.ts:
    import { Component, OnInit } from '@angular/core';
    import { FormBuilder, FormGroup, Validators } from '@angular/forms';
    
    @Component({
    	selector: 'app-root',
    	templateUrl: './app.component.html'
    })
    export class AppComponent implements OnInit {
    	myForm: FormGroup;
    
    	constructor(private fb: FormBuilder) { }
    
    	ngOnInit() {
    		this.myForm = this.fb.group({
    			name: ['', Validators.required],
    			age: ['', Validators.required]
    		});
    	}
    }
    

Running the application will now result in the error: Can't bind to 'formGroup' since it isn't a known property of 'form'.

What is the motivation / use case for changing the behavior?
Basic reactive forms are not working properly anymore.

Please tell us about your environment:
Windows 10 64 bit. Created the project with @angular/cli v1.0.0-beta.30

  • Angular version: 2.3.1 (latest)

  • Browser: all: Chrome 55, Firefox, Internet Explorer 11, Edge 38

  • Language: TypeScript 2.0.3

  • Node (for AoT issues): node --version = 7.5.0

@DzmitryShylovich
Copy link
Contributor

did you import ReactiveFormsModule?

@JPtenBerge
Copy link
Author

JPtenBerge commented Feb 3, 2017

@DzmitryShylovich: I did, I mentioned it with the reproduction steps as well.

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { ReactiveFormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';

import { AppComponent } from './app.component';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    ReactiveFormsModule,
    HttpModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

@DzmitryShylovich
Copy link
Contributor

Pls add a plunkr with repro

@JPtenBerge
Copy link
Author

Ï will, out of time at the moment though, I will add it first thing tomorrow morning. You haven't been able to repro it with angular-cli at your end then?

@DzmitryShylovich
Copy link
Contributor

Nope, I can't reproduce using cli

@jorgjanke
Copy link

works for me using the latest version with the cli - nevertheless, ng-test fails with that very same error - I tried to stub it, but no change - any hints?

@JPtenBerge
Copy link
Author

JPtenBerge commented Feb 4, 2017

@DzmitryShylovich Sigh. Miraculously, everything today suddenly decided to work. Generated another new project and followed the same steps, no problems. Copied code from yesterday's project, no problems. ng serve yesterday's project, no problems. Without making any changes, of course :-)

Never mind I guess. Crisis averted. Thanks anyway though. Should I run into it again, I'll let you know.

@jorgjanke: Did you declare the ReactiveFormsModule as part of your testing module?

beforeEach(() => {
	TestBed.configureTestingModule({
		imports: [ReactiveFormsModule],
		declarations: [YourComponent, ...],
		providers: [...]
	});
});

@DzmitryShylovich
Copy link
Contributor

@JPtenBerge 👍 close the issue then pls. thx

@johnnycamby
Copy link

Hey this issue shouldn't be closed yet. The same error is being raised with version 2.4.8 and non of your solutions are helping.

@Matfork
Copy link

Matfork commented Feb 24, 2017

Im getting the same error in version 2.4.8

component.ts file seems to recongnize FormGroup, FormControl, FormBuilder, Validators, but using [formGroup] inside component.html file throws same error:

"Can't bind to 'formGroup' since it isn't a known property of 'form'."

@rienk-git
Copy link

Getting the same problem with 2.4.8.

After some debugging/trial and error I found that Validators are breaking my code:

This throws the 'Can't bind to 'formGroup' since it isn't a known property of 'form'' - error:

let emailRegex = `([a-zA-Z0-9_.]{1}[a-zA-Z0-9_.]*)((@[a-zA-Z]{2}[a-zA-Z]*)[\\\.]([a-zA-Z]{2}|[a-zA-Z]{3}))`;
this.form = Form.group({
            email: ['', [Validators.required, Validators.pattern(emailRegex)]],
 })

while this does not throw an error:

this.form = Form.group({
            email: ['', [Validators.required]],
 })

So there seems to be a change in way validators are passed or some methods are no longer available or something. The error is very vague and only exposes itself as a template parse error.

I will check the latest docs on validation though.

@Matfork
Copy link

Matfork commented Feb 26, 2017

Ok after some digging I found a solution for "Can't bind to 'formGroup' since it isn't a known property of 'form'."

For my case, I've been using multiple modules files, i added ReactiveFormsModule in app.module.ts

 import { FormsModule, ReactiveFormsModule } from '@angular/forms';`

@NgModule({
  declarations: [
    AppComponent,
  ]
  imports: [
    FormsModule,
    ReactiveFormsModule,
    AuthorModule,
],
...

But this wasn't working when I use a [formGroup] directive from a component added in another module, e.g. using [formGroup] in author.component.ts which is subscribed in author.module.ts file:

import { NgModule }       from '@angular/core';
import { CommonModule }   from '@angular/common';
import { AuthorComponent } from './author.component';

@NgModule({
  imports: [
    CommonModule,
  ],
  declarations: [
    AuthorComponent,
  ],
  providers: [...]
})

export class AuthorModule {}

I thought if i added ReactiveFormsModule in app.module.ts, by default ReactiveFormsModule would be inherited by all its children modules like author.module in this case... (wrong!).
I needed to import ReactiveFormsModule in author.module.ts in order to make all directives to work:

...
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
...

@NgModule({
  imports: [
    ...,
    FormsModule,    //added here too
    ReactiveFormsModule //added here too
  ],
  declarations: [...],
  providers: [...]
})

export class AuthorModule {}

So, if you are using submodules, make sure to import ReactiveFormsModule in each submodule file.
Hope this helps anyone.

@zoechi
Copy link
Contributor

zoechi commented Feb 27, 2017

@Matfork

(wrong!).

Yup, there are no global directives. If components use components, directives, or pipes, the module they are part of need to import these components, directives, or pipes.

Only services provided by modules imported to AppModule become available throughout the whole application.

GitHub issues are for bug reports and feature requests.
For support questions please use other channels like the ones listed in CONTRIBUTING - Got a Question or Problem?

@tsureshpandi
Copy link

After upgrading angular2 to new version i face similar issue.

After lot of changes i got it to work by updating the followings in src/polyfills.ts file.
(by default new angular cli commented these lines, i compare with my old project & update required.)

import 'core-js/es6/symbol';
import 'core-js/es6/object';
import 'core-js/es6/function';
import 'core-js/es6/parse-int';
import 'core-js/es6/parse-float';
import 'core-js/es6/number';
import 'core-js/es6/math';
import 'core-js/es6/string';
import 'core-js/es6/date';
import 'core-js/es6/array';
import 'core-js/es6/regexp';
import 'core-js/es6/map';
import 'core-js/es6/set';

@Marcidius
Copy link

Any solutions to this yet? Running my app is just fine but it's in the spec files I'm having issues. I'm importing ReactiveFormsModule into the TestBed but still, no dice.

@k11k2
Copy link

k11k2 commented May 30, 2017

Followed @Matfork and it works, must import both FormsModule, ReactiveFormsModule .

@rjcorwin
Copy link

rjcorwin commented Jun 1, 2017

I'm in the same boat as @Marcidius

@Phillippe43
Copy link

Phillippe43 commented Jun 6, 2017

Let me give some details.

This problem only occurs running Visual Studio 2017 latest Angular 4 template. They've got multiple modules for client, server, shared. The only way I got it to work in this envioronment was to import the ReactiveFormsModule directly into the component that needed it and changing [formgroup] to (formgroup)

I just tested installing angular/cli and creating a new project from template and it worked as normally expected.

I would say that the problem is with the Visual Studio template I'm using and the way it has framed up their modules.

@Phillippe43
Copy link

Phillippe43 commented Jun 7, 2017

Update, just found out that the problem was with the spa template I was using. They will be correcting the issue in an update.

The work around is to import FormsModule and ReactiveFormsModue into both the app.module.client and app.module.server.

Issue 986

@meetkabeershah
Copy link

meetkabeershah commented Jun 16, 2017

changing [formgroup] to (formgroup)

@Phillippe43 by doing this, you're changing property binding to event binding which changes the purpose in whole

@roshan3133
Copy link

Getting same error in Angular 4.2.1 also
i have used added in app.module.ts as well as in component still i am getting same error.

@vnussolution
Copy link

First check, make sure to import FormsModule, ReactiveFormsModule to @NgModule.
Thanks @Matfork , your solution works for me

@sebichondo
Copy link

@Phillippe43 Thanks, adding the import to both the app.module.client and app.module.server worked for me

@hodglem
Copy link

hodglem commented Aug 8, 2017

I had the same trouble as @rjsteinert and @Marcidius. In my spec I had to add the header import as well as the test bed import. Hope it helps.

import { ReactiveFormsModule } from '@angular/forms';

describe('AddRackAveragesComponent', () => {
  let component: AddRackAveragesComponent;
  let fixture: ComponentFixture<AddRackAveragesComponent>;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports : [
        ReactiveFormsModule
      ],
      declarations: [ AddRackAveragesComponent ]
    })
    .compileComponents();
  }));

@bgies
Copy link

bgies commented Jan 27, 2018

I know this is very late, but I found this issue while searching for an answer to binding properties for custom components in a shared module.

In my case the problem was that my lazy loaded page was still included in the app.module.ts file. Removing that fixed the Can't bind to 'xxx' since it isn't a known property.

Just putting this up for anyone else facing the same issue. It's really just the error message has nothing to do with the actual solution.

@granteagon
Copy link

granteagon commented Jan 30, 2018

I had this also. I had to manually refresh the page after adding ReactiveFormsModule to the imports for my module before I could see the change. LiveReload didn't work.

@SmartBitPixel
Copy link

Imports this line in the module, which declared that component.

import { ReactiveFormsModule, FormsModule } from '@angular/forms';

Example:
If "login.component.ts" is declared in "login.module.ts",
then in "login.module.ts",

write this line:

import { ReactiveFormsModule, FormsModule } from '@angular/forms';

Thanks & Regards,
Arun Dhwaj

@christsantiris
Copy link

The first time I got this error was because I did: [FormGroup] instead of [formGroup] since I was creating it as a new new FormGroup in the component. Making the f lower case fixed it.

@Fernandolcs
Copy link

@ctscodes You are the true hero!

@Martinspire
Copy link

So why is [form-group] not allowed as an attribute value (making the attribute dashed and lowercase). I thought that would be the same as [formGroup] but apparently the error pops up too if you try it

@munapadhi
Copy link

ctscodes replays awasome that also works for me thanks ctscodes:)

@Riyaz0001
Copy link

same problem here.

@victorienfotsing
Copy link

One solution ist :
to use in HTML template I use #formName intead of [FormGroup]="formName"

<form id="userForm" #userForm>
      <input type="text" formControlName="given_name" ... >
      <input type="text" formControlName="family_name" ... >
      <input type="email" formControlName="email" ... >
</form>

app.module.ts

 import { FormsModule, ReactiveFormsModule } from '@angular/forms';`

@NgModule({
  declarations: [
    AppComponent,
  ]
  imports: [
    FormsModule,
    ReactiveFormsModule,
    AuthorModule,
],
...

And the component code look like

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

@Component({
    ...
})
export class ProfileComponent implements OnInit {
  // tslint:disable-next-line:no-input-rename
  @Input('formGroup') userForm: FormGroup = new FormGroup({
    given_name: new FormControl(''),
    family_name: new FormControl(''),
    email: new FormControl('', [
      Validators.required,
      Validators.email,
    ])
  });
  ...
}

It works pretty good 💯

@somvanshi-nitesh
Copy link

Followed @Matfork and it works, must import both FormsModule, ReactiveFormsModule .

@ItsAncientCoder
Copy link

2. ReactiveFormsModule

your answer worked partially in my case.
When i use only 'ReactiveFormsModule', it was giving
"NO provider error'. I have imported both 'FormsModule' and 'ReactiveFormsModule', it started working. Thank you. happy learning.

@metasimian
Copy link

So fiddling around with the same frustrating issue -- a clean angular-cli install and a custom subcomponent/module (component.html... <form [formGroup]="demoForm" (ngSubmit)="onSubmit()">) and the same error ("Can't bind to 'formGroup' since it isn't a known property of 'form' ").
Angular CLI: 7.2.3, Node: 10.9.0, OS: win32 x64, Angular: 7.2.2

I finally got it to work based on the above but with a twist
I put the FormsModule & ReactiveFormsModule imports in app-routing.module.ts (not app.module.ts) + the subcomponent's ts (in my case: forms-demo.component.ts) :

import { FormsModule, ReactiveFormsModule } from '@angular/forms';
....
@NgModule({
  imports: [
    RouterModule.forRoot(routes), 
  	FormsModule, ReactiveFormsModule
....

Afterward ng build/serve worked without any errors.

I'm not an Angular expert but my guess is that the v7 approach whereby app.module.ts delegates to app-routing, that latter file is where the app & component imports & dependencies take place.... YMMV but hopefully it helps.

@NicolasPapp94
Copy link

import { ReactiveFormsModule, FormsModule } from '@angular/forms';

LIKE A CHARM!!!!

@Yurzs
Copy link

Yurzs commented May 19, 2019

For making this work you need to
import { ReactiveFormsModule, FormsModule } from '@angular/forms';in main app.module.ts. Adding them in submodules won't work

@sharmarajat01
Copy link

Ok after some digging I found a solution for "Can't bind to 'formGroup' since it isn't a known property of 'form'."

For my case, I've been using multiple modules files, i added ReactiveFormsModule in app.module.ts

 import { FormsModule, ReactiveFormsModule } from '@angular/forms';`

@NgModule({
  declarations: [
    AppComponent,
  ]
  imports: [
    FormsModule,
    ReactiveFormsModule,
    AuthorModule,
],
...

But this wasn't working when I use a [formGroup] directive from a component added in another module, e.g. using [formGroup] in author.component.ts which is subscribed in author.module.ts file:

import { NgModule }       from '@angular/core';
import { CommonModule }   from '@angular/common';
import { AuthorComponent } from './author.component';

@NgModule({
  imports: [
    CommonModule,
  ],
  declarations: [
    AuthorComponent,
  ],
  providers: [...]
})

export class AuthorModule {}

I thought if i added ReactiveFormsModule in app.module.ts, by default ReactiveFormsModule would be inherited by all its children modules like author.module in this case... (wrong!).
I needed to import ReactiveFormsModule in author.module.ts in order to make all directives to work:

...
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
...

@NgModule({
  imports: [
    ...,
    FormsModule,    //added here too
    ReactiveFormsModule //added here too
  ],
  declarations: [...],
  providers: [...]
})

export class AuthorModule {}

So, if you are using submodules, make sure to import ReactiveFormsModule in each submodule file.
Hope this helps anyone.

lol !! you were the one who dropped out this same comment on stack overflow :p

@dprice000
Copy link

Ok after some digging I found a solution for "Can't bind to 'formGroup' since it isn't a known property of 'form'."

For my case, I've been using multiple modules files, i added ReactiveFormsModule in app.module.ts

 import { FormsModule, ReactiveFormsModule } from '@angular/forms';`

@NgModule({
  declarations: [
    AppComponent,
  ]
  imports: [
    FormsModule,
    ReactiveFormsModule,
    AuthorModule,
],
...

But this wasn't working when I use a [formGroup] directive from a component added in another module, e.g. using [formGroup] in author.component.ts which is subscribed in author.module.ts file:

import { NgModule }       from '@angular/core';
import { CommonModule }   from '@angular/common';
import { AuthorComponent } from './author.component';

@NgModule({
  imports: [
    CommonModule,
  ],
  declarations: [
    AuthorComponent,
  ],
  providers: [...]
})

export class AuthorModule {}

I thought if i added ReactiveFormsModule in app.module.ts, by default ReactiveFormsModule would be inherited by all its children modules like author.module in this case... (wrong!).
I needed to import ReactiveFormsModule in author.module.ts in order to make all directives to work:

...
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
...

@NgModule({
  imports: [
    ...,
    FormsModule,    //added here too
    ReactiveFormsModule //added here too
  ],
  declarations: [...],
  providers: [...]
})

export class AuthorModule {}

So, if you are using submodules, make sure to import ReactiveFormsModule in each submodule file.
Hope this helps anyone.

This helped me. I created my app with AppRouting. It looks like all of the components are created by the AppRouting Module. I had to add ReactiveFormsModule to the AppRouting Module.

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 15, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests