1299

The situation

I am trying to make what should be a very simple form in my Angular application, but no matter what, it never works.

The Angular version

Angular 2.0.0 RC5

The error

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

Enter image description here

The code

The view

<form [formGroup]="newTaskForm" (submit)="createNewTask()">
   <div class="form-group">
      <label for="name">Name</label>
      <input type="text" name="name" required>
   </div>
   <button type="submit" class="btn btn-default">Submit</button>
</form>

The controller

import { Component } from '@angular/core';
import { FormGroup, FormControl, Validators, FormBuilder }  from '@angular/forms';
import {FormsModule,ReactiveFormsModule} from '@angular/forms';
import { Task } from './task';

@Component({
    selector: 'task-add',
    templateUrl: 'app/task-add.component.html'

})
export class TaskAddComponent {

    newTaskForm: FormGroup;

    constructor(fb: FormBuilder)
    {
        this.newTaskForm = fb.group({
            name: ["", Validators.required]
        });
    }

    createNewTask()
    {
        console.log(this.newTaskForm.value)
    }
}

The ngModule

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

import { routing }        from './app.routing';
import { AppComponent }  from './app.component';
import { TaskService } from './task.service'

@NgModule({
    imports: [
        BrowserModule,
        routing,
        FormsModule
    ],
    declarations: [ AppComponent ],
    providers: [
        TaskService
    ],
    bootstrap: [ AppComponent ]
})
export class AppModule { }

The question

Why am I getting that error? Am I missing something?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
FrancescoMussi
  • 20,760
  • 39
  • 126
  • 178
  • 9
    Make sure that you import the ReactiveFormsModule to the module you use forms with ( for instance auth.module , form.module ) not the app.module . https://angular.io/guide/reactive-forms – mercury Jan 27 '21 at 02:51
  • 2
    I wish I could downvote the Angular team for making the design decisions they made – Felipe Oct 19 '21 at 12:32
  • I occasionally get this error message in VS Code, although my code is correct. I just have to open the `xxx.module.ts` file and the problems go away immediately. Maybe someone else have the same problem...might be a bug which will be fixed in the future. – MikhailRatner Jan 27 '22 at 14:25
  • None of the answers below solved my problem. my problem was not adding my component to module declarations. So add your component to `app.module.ts` `declarations: [YourComponent]` – Aven Desta Mar 18 '22 at 05:04
  • If you already have the `ReactiveFormsModule` imported to your component, this error can be a red herring! Be sure you've also added a reference of your module to your app module's `imports`. – jbobbins Nov 22 '22 at 01:02

48 Answers48

2151

RC6/RC7/Final release FIX

To fix this error, you just need to import ReactiveFormsModule from @angular/forms in your module. Here's the example of a basic module with ReactiveFormsModule import:

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { AppComponent }  from './app.component';

@NgModule({
    imports: [
        BrowserModule,
        FormsModule,
        ReactiveFormsModule
    ],
    declarations: [
        AppComponent
    ],
    bootstrap: [AppComponent]
})

export class AppModule { }

To explain further, formGroup is a selector for directive named FormGroupDirective that is a part of ReactiveFormsModule, hence the need to import it. It is used to bind an existing FormGroup to a DOM element. You can read more about it on Angular's official docs page.

RC5 FIX

You need to import { REACTIVE_FORM_DIRECTIVES } from '@angular/forms' in your controller and add it to directives in @Component. That will fix the problem.

After you fix that, you will probably get another error because you didn't add formControlName="name" to your input in form.

Liam
  • 27,717
  • 28
  • 128
  • 190
Stefan Svrkota
  • 48,787
  • 9
  • 98
  • 87
  • 19
    What I don't get is why one needs to add REACTIVE_FORM_DIRECTIVES if FormsModule is being imported inside app.module. The whole point of modules is to avoid having to declare directives inside components. – Daniel Pliscki Sep 01 '16 at 14:21
  • 27
    @DanielPliscki You are completely right, I just found out they fixed this problem in RC6 version that was released today. Now you don't need to do this, you only need to import `FormsModule` and `ReactiveFormsModule`. I will edit my answer. – Stefan Svrkota Sep 01 '16 at 14:24
  • @StefanSvrkota i got this error when i build system in webpack production mode but i build it with a development environment its working fine any idea ? – LittleDragon Mar 09 '17 at 05:47
  • 18
    I LOST AN HOUR completing forgetting that I created a separate module for my login form in order to lazy load with modules between states. (I am new to A2, still prefer A1) Please make sure you use the right module! Don't be a schmuck like me. You also no longer need to add to the component. The imports in your module suffice. Thanks – user1889992 Mar 18 '17 at 23:55
  • 5
    Thanks, worked for me. I am confused why this is not mentioned in the guides for FormControls in Angular 2 that I have stumbled upon.. – cjohansson Apr 19 '17 at 08:14
  • 2
    In Angular 4 I added `ReactiveFormsModule` in the provider list and it worked. Not sure if this is the way you are supposed to do it though. – BrunoLM Jul 18 '17 at 10:55
  • 1
    I would like to add that I ran into this bug because of casing - I had used [FormControl] instead of [formControl] – iJungleBoy Sep 12 '17 at 20:42
  • Thanks ! Works fine with Angular 6 – XenoX Dec 19 '18 at 09:17
  • You also have to do this in every involved submodule.https://github.com/angular/angular/issues/14288#issuecomment-282531453 – Aarón Cárdenas Apr 03 '19 at 00:54
  • 1
    Make sure the change is done in the `imports` section not in the `declarations` – Mauricio Gracia Gutierrez May 08 '19 at 16:22
  • If Not working again int Ionic android development then put it each module of your page. – SHUBHASIS MAHATA Nov 02 '19 at 18:51
  • I am happy to announce this solution is covered by Assistant https://medium.com/@tomaszs2/mapping-stackoverflow-into-real-time-assistance-e627dda88f69 – Tom Smykowski Jul 26 '20 at 08:55
  • Thanks! In Angular 12 you only need to import ReactiveFormsModule and add it into imports. – MikhailRatner Nov 02 '21 at 12:47
  • I still occasionally get this error message in VS Code, although my code is correct. I just have to open the `xxx.module.ts` file and the problems go away immediately. Maybe someone else have the same problem...might be a bug which will be fixed in the future. – MikhailRatner Jan 27 '22 at 14:26
202

Angular 4 in combination with feature modules (if you are for instance using a shared-module) requires you to also export the ReactiveFormsModule to work.

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';

@NgModule({
  imports:      [
    CommonModule,
    ReactiveFormsModule
  ],
  declarations: [],
  exports: [
    CommonModule,
    FormsModule,
    ReactiveFormsModule
  ]
})
export class SharedModule { } 
Sahil
  • 1,387
  • 14
  • 41
Undrium
  • 2,558
  • 1
  • 16
  • 24
181

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.

starball
  • 20,030
  • 7
  • 43
  • 238
Ashutosh Jha
  • 15,451
  • 11
  • 52
  • 85
  • 2
    Works for me, exactly the same problem, I really thought modules in exports array will be inherited by children modules... Don't know why exactly ! EDIT : [documentation says](https://angular.io/api/core/NgModule#exports) exports is to make components, pipes, directives available in the TEMPLATE of any COMPONENT () – guy777 Nov 22 '18 at 21:24
  • This is **the solution** if you use **lazy loading** or work with **multiple modules**. Thanks. – Murat Yıldız Oct 03 '20 at 18:18
70

I have encountered this error during unit testing of a component (only during testing, within application it worked normally). The solution is to import ReactiveFormsModule in .spec.ts file:

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

describe('MyComponent', () => {
    beforeEach(async(() => {
        TestBed.configureTestingModule({
            declarations: [MyComponent],
            imports: [ReactiveFormsModule],  // Also add it to 'imports' array
        })
        .compileComponents();
    }));
});
xuhcc
  • 2,400
  • 24
  • 23
35
  1. go to: app.module.ts
  2. imports:[ .... ReactiveFormsModule ]
  3. add this: import { ReactiveFormsModule } from '@angular/forms';
  4. it should look like that:

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

@NgModule({
  declarations: [
  
  ],
  imports: [
    
    ReactiveFormsModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }
Mounir bkr
  • 1,069
  • 6
  • 6
29

The error says that FormGroup is not recognized in this module. So you have to import these (below) modules in every module that uses FormGroup

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

Then add FormsModule and ReactiveFormsModule into your Module's imports array.

imports: [
  FormsModule,
  ReactiveFormsModule
],

You may be thinking that I have already added it in AppModule and it should inherit from it? But it is not. Because these modules are exporting required directives that are available only in importing modules. Read more in Sharing modules.

Other factors for these errors may be be a spelling error like below...

[FormGroup]="form" Capital F instead of small f

[formsGroup]="form" Extra s after form

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Abhishek Singh
  • 1,631
  • 1
  • 17
  • 31
27

The suggested answer did not work for me with Angular 4. Instead I had to use another way of attribute binding with the attr prefix:

<element [attr.attribute-to-bind]="someValue">
str
  • 42,689
  • 17
  • 109
  • 127
  • 4
    Hey man! Are you sure your answer is related to the question? :) The question was about an issue setting up a form - cause by not properly setting up the ngModule – FrancescoMussi Apr 26 '17 at 14:24
  • 1
    @johnnyfittizio Pretty sure. Same scenario, same error message. – str Apr 26 '17 at 14:47
  • 2
    This worked for me, but is strange - why do I need the `attr.` ? – CodyBugstein Oct 26 '17 at 21:17
  • Thanks a ton. This worked for me also, but I would think there must be something else triggering the problem, like library versioning, or
    tag positioning? Strange.
    – Memetican Mar 14 '19 at 23:33
  • Found it- the problem was that I needed to import `ReactiveFormsModule` directly into my page's `.module.ts`. Not the `.page.ts`... once I did that, my template could correctly identify the `formGroup` attribute, without the `attr.` prefix. – Memetican Mar 15 '19 at 00:37
20

If you have to import two modules then add like this below

import {ReactiveFormsModule,FormsModule} from '@angular/forms';
@NgModule({
  declarations: [
    AppComponent,
    HomeComponentComponent,
    BlogComponentComponent,
    ContactComponentComponent,
    HeaderComponentComponent,
    FooterComponentComponent,
    RegisterComponent,
    LoginComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    routes,
    ReactiveFormsModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
sudheer nunna
  • 1,659
  • 15
  • 17
  • Jeez Angular, despite using `ng g c myComponent` the component had not been added to the declarations section app.module.ts, after adding it the app compiles. How dumb is this? – Rin and Len Aug 09 '22 at 10:01
20

Firstly, it is not related to Angular versions>2. Just import the following in your app.module.ts file will fix the problems.

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

Then add FormsModule and ReactiveFormsModule into your imports array.

imports: [
  FormsModule,
  ReactiveFormsModule
],

Note: You can also import ReactiveFormsModule to a specific module instead to app.module.ts

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
17

Keep in mind that if you have defined "Feature Modules", you'll need to import in the Feature Module, even if you already imported to the AppModule. From the Angular documentation:

Modules don't inherit access to the components, directives, or pipes that are declared in other modules. What AppModule imports is irrelevant to ContactModule and vice versa. Before ContactComponent can bind with [(ngModel)], its ContactModule must import FormsModule.

https://angular.io/docs/ts/latest/guide/ngmodule.html

Ian Griffin
  • 171
  • 1
  • 4
  • 1
    thank you so much! I was working for hours on this issue, can't imagine what the problem was... Simply importing my feature Module to app Module fixed the issue. – keschra Nov 24 '20 at 10:44
  • This was my issue as well. Missed importing into a module. Thanks!! – Scott Fraley May 05 '23 at 21:44
14

I had the same issue with Angular 7. Just import following in your app.module.ts file.

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

Then add FormsModule and ReactiveFormsModule in to your imports array.

imports: [
  FormsModule,
  ReactiveFormsModule
],
Chamila Maddumage
  • 3,304
  • 2
  • 32
  • 43
12

This problem occurs due to missing import of FormsModule,ReactiveFormsModule .I also came with same problem. My case was diff. as i was working with modules.So i missed above imports in my parent modules though i had imported it into child modules,it wasn't working.

Then i imported it into my parent modules as below, and it worked!

import { ReactiveFormsModule,FormsModule  } from '@angular/forms';
import { AlertModule } from 'ngx-bootstrap';

         @NgModule({
          imports: [
            CommonModule,
            FormsModule,
            ReactiveFormsModule,
    ],
      declarations: [MyComponent]
    })
Saurav
  • 392
  • 4
  • 4
10
  1. Your Angular version is 11+, and you use VisualStudioCode?

  2. And you have already imported FormsModule, ReactiveFormsModule and added it into your imports-section within e.g. app.module.ts (relevant module can be different, choose the right one):

// app.module.ts (excerpt)
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
imports: [
  ...
  FormsModule,
  ReactiveFormsModule,
  ...
],
  1. You have the right imports (sometimes there are other libs with similar names); you have defined and initialized your form in your component?
// MyWonderfulComponent (excerpt)


import { FormBuilder, FormGroup, Validators } from '@angular/forms';

export class MyWonderfulComponent implements OnInit {
  form: FormGroup; 
  ...
  constructor (private fb: FormBuilder) {
    this.form = this.fb.group({
      // DON'T FORGET THE FORM INITIALISATION
    });
  }
  1. Your Component-Template has your form:
<form [formGroup]="form" (ngSubmit)="submit()">
  <!-- MY FORM CONTROLS ARE ALREADY HERE --> 
</form>
  1. And you still get the error message "...since it isn't a known property of..." ?

then just simple restart your VisualStudioCode :)

  • Note that you need to import `FormsModule/ReactiveFormsModule` in the module that the component is actually a part of. Blindly putting those in `app.module.ts` is not enough. – Tamius Han Jul 08 '21 at 13:23
  • 1
    This solution also worked for me in PhpStorm. After a restart it didn't give the warning anymore. – Wilco Feb 17 '22 at 14:11
9

Don't be a dumb dumb like me. I was getting the same error as above, and none of the options in previous answers worked. Then I realized I capitalized 'F' in FormGroup. Doh!

Instead of:

[FormGroup]="form"

Do:

[formGroup]="form"
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
flyer
  • 1,414
  • 12
  • 13
9

Simple solution:

Step 1: Import ReactiveFormModule

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

Step 2: Add "ReactiveFormsModule" to the import section

imports: [

    ReactiveFormsModule
  ]

Step 3: Restart the application and done

Example:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import {ReactiveFormsModule} from '@angular/forms';
import { EscalationManagementRoutingModule } from './escalation-management-routing.module';
import { EscalationManagementRouteWrapperComponent } from './escalation-management-route-wrapper.component';


@NgModule({
  declarations: [EscalationManagementRouteWrapperComponent],
  imports: [
    CommonModule,
    EscalationManagementRoutingModule,
    ReactiveFormsModule
  ]
})
export class EscalationManagementModule { }
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Shashwat Gupta
  • 5,071
  • 41
  • 33
8

I was coming across this error when trying to do e2e testing and it was driving me crazy that there were no answers to this.

IF YOU ARE DOING TESTING, find your *.specs.ts file and add :

import {ReactiveFormsModule, FormsModule} from '@angular/forms';
Mr Giggles
  • 2,483
  • 3
  • 22
  • 35
  • refer this answer for more clarity how you will add that for unit testing: [@xuhcc](https://stackoverflow.com/a/47559288/15063341) – Visrut Mar 03 '23 at 12:32
7

For people strolling these threads about this error. In my case I had a shared module where I only exported the FormsModule and ReactiveFormsModule and forgot to import it. This caused a strange error that formgroups were not working in sub components. Hope this helps people scratching their heads.

GKooij
  • 71
  • 1
  • 1
7

If you have this problem when you are developing a component, you should add these two modules to your closest module:

import { FormsModule, ReactiveFormsModule } from '@angular/forms';
@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
   // other modules
    FormsModule,
    ReactiveFormsModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

And if you are developing a test for your components so you should add this module to your test file like this:

import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { ContactusComponent } from './contactus.component';
import { ReactiveFormsModule } from '@angular/forms';

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

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [ContactusComponent],
      imports:[
        ReactiveFormsModule
      ]

    })
      .compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(ContactusComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });
});
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Hamid
  • 761
  • 7
  • 18
7

add "ReactiveFormsModule" in your <componenet.module.ts> parent

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

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

    ]
})
export class InvoiceModule { }
Mounir bkr
  • 1,069
  • 6
  • 6
6

A LITTLE NOTE: Be careful about loaders and minimize (Rails env.):

After seeing this error and trying every solution out there, i realised there was something wrong with my html loader.

I've set my Rails environment up to import HTML paths for my components successfully with this loader (config/loaders/html.js.):

module.exports = {
  test: /\.html$/,
  use: [ {
    loader: 'html-loader?exportAsEs6Default',
    options: {
      minimize: true
    }
  }]
}

After some hours efforts and countless of ReactiveFormsModule imports i saw that my formGroup was small letters: formgroup.

This led me to the loader and the fact that it downcased my HTML on minimize.

After changing the options, everything worked as it should, and i could go back to crying again.

I know that this is not an answer to the question, but for future Rails visitors (and other with custom loaders) i think this could be helpfull.

6

Import and register ReactiveFormsModule in your app.module.ts.

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

@NgModule({
declarations: [
AppComponent,
HighlightDirective,
TestPipeComponent,
ExpoentialStrengthPipe

],
imports: [
BrowserModule,
ReactiveFormsModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }

Make sure your spelling is correct in both .ts and .html file. xxx.ts

  profileForm = new FormGroup({
  firstName: new FormControl(''),
 lastName: new FormControl('')
 });

xxx.html file-

  <form [formGroup]="profileForm"> 
  <label>
  First Name:
   <input type="text" formControlName = "firstName">
  </label>

  <label>
  Last Name:
   <input type="text" formControlName = "lastName">
   </label>
   </form>

I was by mistake wrote [FormGroup] insted of [formGroup]. Check your spelling correctly in .html. It doesn't throw compile time error If anything wrong in .html file.

6

Using and import REACTIVE_FORM_DIRECTIVES:

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { AppComponent }  from './app.component';

@NgModule({
    imports: [
        BrowserModule,
        FormsModule,
        ReactiveFormsModule
    ],
    declarations: [
        AppComponent
    ],
    bootstrap: [AppComponent]
})

export class AppModule { }
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
mojtaba ramezani
  • 1,461
  • 16
  • 15
6

I had the same problem. Make sure that if using submodules (for example, you not only have app.component.module.ts), but you have a separate component such as login.module.ts, that you include ReactiveFormsModule import in this login.module.ts import, for it to work. I don't even have to import ReactiveFormsModule in my app.component.module, because I'm using submodules for everything.

File login.module.ts

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';

import { IonicModule } from '@ionic/angular';

import { LoginPageRoutingModule } from './login-routing.module';

import { LoginPage } from './login.page';

@NgModule({
  imports: [
    CommonModule,
    FormsModule,
    ReactiveFormsModule,
    IonicModule,
    LoginPageRoutingModule
  ],
  declarations: [LoginPage]
})
export class LoginPageModule {}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Peter Edwards
  • 116
  • 1
  • 3
  • that was also a problem in my case. I had enter-credentials-modal-component and login-site-component. it did not work until I did import of FormsModule and ReactiveFormsModule for both of them – Jacob Mar 22 '21 at 22:55
6

I tried almost all the solution here but my problem was a little different(stupid). I added the component in routing module but didn't include it main module. So make sure your component is part of the module.

manjeet lama
  • 185
  • 3
  • 12
  • If your component is a child component, don't forget to add `import { FormsModule, ReactiveFormsModule } from '@angular/forms';` to its respective module. – Bjorn Liza Aug 25 '21 at 14:52
6

The ReactiveFormsModule and FormsModule import should be added in your custom component module and also its parent component from where it is getting called.

For example, I needed to add form for my filter component. So I should add this import in my filter module and its parent page (might be list) module from where this filter button gets clicked.

6

Error ex:error NG8002: Can't bind to 'formGroup' since it isn't a known property of 'form'.

I am giving possible reasons and solutions if the solution works/useful please upvote and make a comment which solution worked for you

Scenario 1 and suggestion: if you have only one module or the error appears in app component

You have to import the FormsModule, ReactiveFormsModule in the module

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

imports: [
  FormsModule,
  ReactiveFormsModule,
],

Scenario 2 and suggestion: when your application contains multiple modules

Import FormsModule and reactiveFormsModule in that particular module/submodule i.e. nearest module

Scenario 3 and suggestion: If you imported FormModule and ReactiveFormModule correctly there might be another problem.

Maybe you didn't add your component to module declarations in the app module. So add your component to module declarations.

import { YourComponentName } from './example-file-path/your-component-name.component';

@NgModule({
  declarations: [
    YourComponentName
  ]
})

Scenario 4 and suggestion: If you added ReactiveFormsModule and FormsModule as mentioned in the above.

Try to reload the project (restart the IDE) and start your server once again with ng serve

.

Srikrushna
  • 4,366
  • 2
  • 39
  • 46
5

Note: if you are working inside child module's component, then you just have to import ReactiveFormsModule in child module rather than parent app root module.

nik7
  • 806
  • 3
  • 12
  • 20
mabdullahse
  • 3,474
  • 25
  • 23
5

Import ReactiveFormsModule in the corresponding module.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Meghnath Das
  • 145
  • 1
  • 6
5

If this is just a TypeScript error but everything on your form works, you may just have to restart your IDE.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Avi E. Koenig
  • 360
  • 5
  • 13
4

When you have a formGroup in a modal (entrycomponent), then you have to import ReactiveFormsModule also in the module where the modal is instantiated.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
tonertop
  • 51
  • 2
3

import { FormsModule, ReactiveFormsModule } from '@angular/forms'; and add it in imports array in the app-module.ts file.

3

You can get this error message even if you have already imported FormsModule and ReactiveFormsModule. I moved a component (that uses the [formGroup] directive) from one project to another, but failed to add the component to the declarations array in the new module. That resulted in the Can't bind to 'formGroup' since it isn't a known property of 'form' error message.

Trevor
  • 13,085
  • 13
  • 76
  • 99
  • have you tried to reload the project with an ng serve after the modification? (if you have embedded modules, you must import the form ans reactive form modules in each and every module using formGroup) – Oumaima Abouzaid Sep 24 '20 at 10:29
3

You need to import the FormsModule, ReactiveFormsModule in this module as well as the top level.

If you used a reactiveForm in another module then you've to do also this step along with above step: import also reactiveFormsModule in that particular module.

For example:

imports: [
  BrowserModule,
  FormsModule,
  ReactiveFormsModule,
  AppRoutingModule,
  HttpClientModule,
  BrowserAnimationsModule
],
Zsolt Meszaros
  • 21,961
  • 19
  • 54
  • 57
Rohit
  • 101
  • 7
3

Three main points we have to focus on if encountered such types of issues.

  1. Import ReactiveFormsModule, FormsModule in any shared or app module.
  2. If you created a shared module then import that into a module on which you are using the form group and
  3. check that component is in declarations: [ ProjectComponent ] like this.
Viplav Soni
  • 1,489
  • 13
  • 18
3

If you imported FormModule and ReactiveFormModule correctly there might be another problem. Maybe you didn't add your component to module declarations in the app module. So add your component to app.module.ts declarations:

@NgModule({
  declarations: [
    YourNewComponent
  ]
})
Ramil Mammadov
  • 462
  • 6
  • 11
2

I've been struggling with this error during the last 3 days. I added the ReactiveFormsModule and FormsModule as mentionned in the comments above in both of my modules but it had no effect until I reloaded my project with another "ng serve". I don't know why it didn't reload automatically but at least i'm glad it worked finally! Any explanation please?

  • 2
    See this: https://stackoverflow.com/questions/49884563/angular-how-to-fix-property-does-not-exist-on-type-error. It happened because sometimes your code is not transpiled and you are seeing old code. – Gaurav Chauhan Oct 01 '20 at 06:12
2

You can import formModule and reactiveFormModule

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


@NgModule({
    declarations: [
        ContactComponent
    ],
    imports: [
        CommonModule,
        ContactRoutingModule,
        FormsModule,
        ReactiveFormsModule
    ]
})
export class ContactModule { }
G.S
  • 10,413
  • 7
  • 36
  • 52
2

For Angular 14 and who uses sub-modules with routing

My issue was related to using sub-modules with routing. To make it works, follow these three steps:

1- In app.module

imports: [
//other-modules
ReactiveFormsModule

]

2- In sub-module

imports: [
//other-modules
ReactiveFormsModule

]

3- In app-routing.module add the following routing (my module was named AuthModule)

{
path: 'signup',
component: SignUpComponent,
children: [
  {
    path: '',
    loadChildren: () =>
      import('./features/auth/auth.module').then((m) => m.AuthModule),
  },
]

},

Hope this help somebody someday!

Mohamed Mohsen
  • 167
  • 1
  • 13
1

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

means that you try to bind a property using angular ([prop]) but angular cant find anything he knows for that element (in this case form).

this can happen by not using the right module (missing an import somewhere in the way) and sometimes just cause a typo like:

[formsGroup], with s after form

bresleveloper
  • 5,940
  • 3
  • 33
  • 47
1

import below line in app.modual.ts file and run command ng s -o in angular cli.

import { FormsModule, ReactiveFormsModule } from '@angular/forms';
Deven
  • 684
  • 7
  • 10
1

If,
Already Imported

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

in rootModule & customComponentModule & still getting the error

NG8002: Can't bind to 'formGroup' since it isn't a known property of 'form'

just,

Restart you Application server

this will solve the issue

1

in case you are using standalone components, import ReactiveFormsModule in the same component.ts file and in the @Component decorator, add a property called imports and give it an array containing the ReactiveFormsModule as an element. Like so:

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

@Component({
  selector: 'app-signin',
  standalone: true,
  templateUrl: './signin.component.html',
  styleUrls: ['./signin.component.css'],
  imports: [ReactiveFormsModule]
})
export class SigninComponent {
  signinForm = new FormGroup({
    email: new FormControl(''),
    password: new FormControl('')
  });
  
}
0

My solution was subtle and I didn't see it listed already.

I was using reactive forms in an Angular Materials Dialog component that wasn't declared in app.module.ts. The main component was declared in app.module.ts and would open the dialog component but the dialog component was not explicitly declared in app.module.ts.

I didn't have any problems using the dialog component normally except that the form threw this error whenever I opened the dialog:

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
NDavis
  • 1,127
  • 2
  • 14
  • 23
0

I had the same issue and the problem was in fact only that I forgot to import and declare the component which holds the form in the module:

import { ContactFormComponent } from './contact-form/contact-form.component';

@NgModule({
    declarations: [..., ContactFormComponent, ...],
    imports: [CommonModule, HomeRoutingModule, SharedModule]
})
export class HomeModule {}
yglodt
  • 13,807
  • 14
  • 91
  • 127
0

You need to import the FormsModule, ReactiveFormsModule in this module

If you used a reactiveForm in another module then you've to do also this step along with above step: import also reactiveFormsModule in that particular module.

For example:

imports: [
  FormsModule,
  ReactiveFormsModule,
  
],
Naeem Bashir
  • 1,937
  • 20
  • 17
0

I did not see this particular failure mechanism described here or any reference to public-api.ts elsewhere within this issue, so adding it in case it helps someone else.

I have a component built using Reactive Forms inside a feature module. Following the guidance described in other answers, my module built without issue UNTIL I attempted to export the component via public-api.ts. At that point, I began to get the familiar error Can't bind to 'formGroup' since it isn't a known property of 'form', with no other indication what might be wrong.

This issue occurred for me at this point because I forgot to declare and export the component from the NgModule. Once I added it there, the module began to build again. So, three things to remember in the NgModule when attempting to export a Reactive Forms component:

import { NgModule } from '@angular/core';
import { ReactiveFormsModule } from '@angular/forms';
import { MyExportedComponent } from './components/my-exported.component';


@NgModule({
    declarations: [
        MyExportedComponent // this
  ],
    imports: [
        ReactiveFormsModule // this
    ], 
    exports: [
        MyExportedComponent // and this
    ],
    providers: [
        ]
})
export class MyModule { }

At which point, Angular will play nice with this line in public-api.ts:

export * from './lib/my-module/components/my-exported.component';
tek
  • 343
  • 1
  • 4
  • 10
0

I had this issue and neither the imports nor restarting ng serve were the problem. In my case, the problem was that I had copied some Angular 8 code into a new project, which used the FormBuilder class to build form groups.

However in newer Angular versions, FormBuilder is a typed class. Changing to UntypedFormBuilder got the app to compile successfully. Every component in the module was reporting errors with basic bindings, not only [formGroup] but also [routerLink] and [ngClass], but I only had to fix the FormBuilder type in a single component for all of those to go away.

R. Salisbury
  • 1,954
  • 16
  • 17
-1

Say you have structured your app like below:

  • AnotherModule
    • <your_form>
  • AppModule

Import ReactiveFormsModule or FormsModule in AnotherModule based on your form type.

Make sure AnotherModule is imported in AppModule.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Leandro
  • 1,244
  • 16
  • 28