302

I keep getting this error while using TypeScript's Angular2-forms framework:

There is no directive with "exportAs" set to "ngForm"

Here's my code

project dependencies :

 "dependencies": { "@angular/common": "2.0.0-rc.6", "@angular/compiler": "2.0.0-rc.6", "@angular/core": "2.0.0-rc.6", "@angular/forms": "2.0.0-rc.6", "@angular/http": "2.0.0-rc.6", "@angular/platform-browser": "2.0.0-rc.6", "@angular/platform-browser-dynamic": "2.0.0-rc.6", "@angular/router": "3.0.0-rc.2", "ng2-bootstrap": "^1.1.1", "reflect-metadata": "^0.1.8", "core-js": "^2.4.0", "es6-module-loader": "^0.17.8", "rxjs": "5.0.0-beta.11", "systemjs": "0.19.27", "zone.js": "0.6.17", "jquery": "3.0.0", } 

And this is the Login Template :

<form #loginForm="ngForm" (ng-submit)="authenticate(loginForm.value)"> </form> 

...and the Login Component :

import { Component } from '@angular/core'; import {Http, Headers} from '@angular/http'; @Component({ moduleId: module.id, selector: 'login-cmp', templateUrl: 'login.component.html' }) export class LoginComponent { constructor($http: Http) { this.$http = $http; } authenticate(data) { ... } } 

I have this Error :

zone.js?1474211973422:484 Unhandled Promise rejection: Template parse errors: There is no directive with "exportAs" set to "ngForm" (" <form [ERROR ->]#loginForm="ngForm" (ngsubmit)="authenticate(loginForm.value)"> 

    28 Answers 28

    590
    import { FormsModule } from '@angular/forms'; @NgModule({ imports: [ BrowserModule, FormsModule //<----------make sure you have added this. ], .... }) 
    7
    • 48
      I've done this and I still get the error. Any other ideas? (I'm on the release version.)CommentedOct 13, 2016 at 14:16
    • 46
      It must be bound to <form> element
      – pop
      CommentedFeb 23, 2017 at 9:17
    • 12
      my problem solved with add ReactiveFormsModule in my module.CommentedMar 15, 2018 at 21:16
    • <form #employeeForm="ngForm" class="emp-form" write properties you want it >CommentedJul 31, 2018 at 15:56
    • 1
      Make sure your component in the declarations in the app.module.tsCommentedJan 18, 2023 at 6:58
    69

    You have to import FormsModule into not only the root AppModule, but also into every subModule that uses any angular forms directives.

    // SubModule A import { CommonModule } from '@angular/common'; import { FormsModule } from '@angular/forms'; @NgModule({ imports: [ CommonModule, FormsModule //<----------make sure you have added this. ], .... }) 
    3
    • 1
      You can add to export array, then you'll not need to add it to multiple submodules imports
      – Samih A
      CommentedJan 2, 2018 at 13:02
    • 1
      @SamihA Please explain with example, the export array goes in AppModule or Submodule A?
      – TetraDev
      CommentedJan 2, 2018 at 17:22
    • Also don't forget to import ReactiveFormsModule
      – Snedden27
      CommentedMar 12, 2018 at 21:10
    58

    I have added ReactiveFormsModule in imports[] array to resolve this error.

    Error: There is no directive with "exportAs" set to "ngForm" ("

    Fix:

    module.ts:

    import {FormsModule, ReactiveFormsModule} from '@angular/forms' imports: [ BrowserModule, FormsModule , ReactiveFormsModule ], 
    1
    • Also be aware that component that uses this directives is declared(in declaration property of NgModule) besides FormsModule import
      – Anton
      CommentedSep 2, 2021 at 23:43
    19
    import { FormsModule } from '@angular/forms'; @NgModule({ imports: [FormsModule], ... }) 
    0
      16

      (Just in case someone else is blind like me) formFTW! Make sure to use <form> tag

      wont work:

      <div (ngSubmit)="search()" #f="ngForm" class="input-group"> <span class="input-group-btn"> <button class="btn btn-secondary" type="submit">Go!</button> </span> <input type="text" ngModel class="form-control" name="search" placeholder="Search..." aria-label="Search..."> </div> 

      works like a charm:

       <form (ngSubmit)="search()" #f="ngForm" class="input-group"> <span class="input-group-btn"> <button class="btn btn-secondary" type="submit">Go!</button> </span> <input type="text" ngModel class="form-control" name="search" placeholder="Search..." aria-label="Search..."> </form> 
        11

        Another item check:

        Make sure you component is added to the declarations array of @NgModule in app.module.ts

        @NgModule({ declarations: [ YourComponent, ], 

        When running the ng generate component command, it does not automatilly add it to app.module.

        0
          10

          In case a name is assigned like this:

          #editForm="testForm" 

          ... the exportAs has to be defined in the component decorator:

          selector: 'test-form', templateUrl: './test-form.component.html', styleUrls: ['./test-form.component.scss'], exportAs: 'testForm' 
          0
            10

            This

            <div #myForm="ngForm"></div> 

            Also causes the error and can be fixed by including the ngForm directive.

            <div ngForm #myForm="ngForm"></div> 
            0
              7

              check whether you import FormsModule. There's no ngControl in the new forms angular 2 release version. you have to change your template to as an example

              <div class="row"> <div class="form-group col-sm-7 col-md-5"> <label for="name">Name</label> <input type="text" class="form-control" required [(ngModel)]="user.name" name="name" #name="ngModel"> <div [hidden]="name.valid || name.pristine" class="alert alert-danger"> Name is required </div> </div> </div> 
              0
                7

                Three Things you have to care:

                1. If you're using the sub-module, you have to import the FormModule in that sub module:

                  imports:[CommonModule, HttpModule, FormsModule] 
                2. You have to exports the FormModule in the module:

                  exports:[FormsModule] 

                  so together it looks like:

                  imports:[CommonModule, HttpModule, FormsModule], exports:[FormsModule], 
                3. In the top of the file you have to import the FormsModule:

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

                If you are using only the app.module.ts then no need to export. Only import required.

                1
                • 1
                  If you use FormsModule within your own module you need the following in your xxxx.modules.ts. Import Forms from Angular: import { FormsModule } from '@angular/forms'; Add it to the imports array NgModule: @NgModule({ imports: [ FormsModule ],CommentedFeb 1, 2018 at 12:35
                5

                In addition to import the form module in login component ts file you need to import NgForm also.

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

                This resolved my issue

                0
                  4

                  I've come to this same question over & over again, also due to same reason. So let me answer this by saying what wrong I was doing. Might be helpful for someone.

                  I was creating component via angular-cli by command

                  ng g c components/something/something

                  What it did, was created the component as I wanted.

                  Also, While creating the component, it added the component in the App Module's declarations array.

                  If this is the case, please remove it. And Voila! It might work.

                    3
                    import { FormsModule,ReactiveFormsModule }from'@angular/forms'; imports:[ BrowserModule.withServerTransition({ appId: 'ng-cli-universal' }), HttpClientModule, FormsModule, ReactiveFormsModule/*This one in particular*/,... ], 

                    in app.module.ts to permanently solve errors like "cannot bind [formGroup] or no directive with export as".

                      3

                      Yes i faced same problem...I did above all things but not worked. This one solved my problem. Because we are using angular 'with' in strict mode.

                      in app.module.ts add this code

                      import {FormsModule, ReactiveFormsModule} from '@angular/forms' imports: [ BrowserModule, FormsModule, ReactiveFormsModule ], 

                      and which file you are using NgForm in .html file

                      <form #form="ngForm" (ngSubmit)="loginSubmit(form);"> </form> 

                      and in .ts file

                      import { NgForm } from '@angular/forms'; @Component({ selector: 'app-login-page', templateUrl: './login-page.component.html', styleUrls: ['./login-page.component.scss'] }) export class LoginPageComponent implements OnInit { NgForm=NgForm;//this one solve my problem...initialization and declaration } 
                        3

                        You must declare FormsModule and must declare also the Compoponent

                        declarations: [ YourComponent --> ], imports: [ BrowserModule, FormsModule, --> ], 
                        2
                        • This was the case for me. I add FormsModule but did not add my component in the AppModule and it was giving me error. thanksCommentedSep 20, 2021 at 21:10
                        • i have added this line import { FormsModule } from '@angular/forms'; in app.module.ts and in app.componenet.html <form #signUpForm="ngForm" (ngSubmit)="onSubmit(signUpForm)"> </form> create onSubmit() function in app.component.ts now my form is working fineCommentedJun 15, 2024 at 14:34
                        3

                        If you already import FormsModule, then you just need to remove formControlName='whatever' from the input fields.

                          2

                          This error also occurs if you are trying to write a unit test case in angular using jasmine.

                          The basic concept of this error is to import FormsModule. Thus, in the file for unit tests, we add imports Section and place FormsModule in that file under

                           TestBed.configureTestingModule For eg: TestBed.configureTestingModule({ declarations: [ XYZComponent ], **imports: [FormsModule]**, }).compileComponents(); 
                            2

                            In my case, I forgot to add my component in the Declaration array of app.module.ts, and voila! the issue was fixed.

                              2

                              Almost all the answers talk about adding the FormsModule module but in my case the import is already done. As said in an answer to the question, you have to close and restart VScode or in my case you have to save your workspace with "Save workspace as ..." and everything will work normally again.

                              2
                              • Please add further details to expand on your answer, such as working code or documentation citations.
                                – CommunityBot
                                CommentedSep 6, 2021 at 1:40
                              • Very good but this also did not work. error is there. thanksCommentedSep 20, 2021 at 21:08
                              2

                              If you have already imported and added the FormsModule to the Sub Module that you are going to use, and still facing the error,

                              1. You may missed it to add the root AppModule. (silly but it happens sometimes)2. Make sure you have added relevant package and referenced it in systemjs.config.js

                                2

                                In your app.module.ts

                                Make sure,

                                1. FormsModule in imports

                                2. SampleComponent in declarations

                                  2

                                  None of the other answers worked for me (Angular 16.2.7) but this did. Change the HTML element in the template from this:

                                  <form #myForm="ngForm" > 

                                  To either this:

                                  <form #myForm ngForm > 

                                  Or this:

                                  <form myForm="ngForm" > 

                                  These are from this GitHub post about the issue.

                                    1

                                    change <form #loginForm="ngForm" > to <form #loginForm ngForm >

                                      0

                                      You should terminate app with ctrl+c and re run it with ng serve, if you did not include FormsModule into you app.module file imports array, and then added it later, angular does not know it, it does not re-scan modules, you should restart app so angular could see that new module is included, after what it will included all features of template drive approach

                                        0

                                        In my case I had to remove the ngNoForm attribute from my <form> tag.

                                        If you you want to import FormsModule in your application but want to skip a specific form, you can use the ngNoForm directive which will prevent ngForm from being added to the form

                                        Reference: https://www.techiediaries.com/angular-ngform-ngnoform-template-reference-variable/

                                          0

                                          I just moved routing modules i.e. say ARoutingModule above FormsModule and ReactiveFormsModule and after CommonModule in imports array of modules.

                                            0

                                            Agree with the solution provided by @micronyks but that hold's true if you've simple application which doesn't have many modules within it. Otherwise, we need to add in similar way to module where we're using that <form #loginForm="ngForm"> element and adding below code inside that module if you don't want to expose it to all modules or whole application.

                                            import FormsModule from '@angular/forms'; @NgModule{ imports: [FormsModule], } 
                                              0

                                              If you are using directive. make sure you add exportAs: 'something' with selector

                                                Start asking to get answers

                                                Find the answer to your question by asking.

                                                Ask question

                                                Explore related questions

                                                See similar questions with these tags.