There is no directive with the parameter "exportAs" set to "ngForm",

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 entry template:

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

And this input 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)"> 
+166
angular angular2-forms
Sep 18 '16 at 15:23
source share
13 answers
 import { FormsModule } from '@angular/forms'; @NgModule({ imports: [ BrowserModule, FormsModule //<----------make sure you have added this. ], .... }) 
+365
Sep 18 '16 at 15:25
source share

You need to import FormsModule not only in the root AppModule, but also in every subModule that uses any angular form directives.

 // SubModule A import { CommonModule } from '@angular/common'; import { FormsModule } from '@angular/forms'; @NgModule({ imports: [ CommonModule, FormsModule //<----------make sure you have added this. ], .... }) 
+39
Aug 09 '17 at 21:35
source share

I know this is an old post, but I would like to share my decision. I added " ReactiveFormsModule " in the import [] array to fix this error

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

Fix:

module.ts

import {FormsModule, ReactiveFormsModule } of '@ angular / forms'

  imports: [ BrowserModule, FormsModule , ReactiveFormsModule ], 
+31
Jan 31 '18 at 19:27
source share
 import { FormsModule } from '@angular/forms'; @NgModule({ imports: [FormsModule], ... }) 
+12
Sep 18 '16 at 15:24
source share

(Just in case someone is blind, like me) form FTW ! Be sure to use the <form> tag <form>

does not 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> 
+6
Sep 27 '17 at 8:21 on
source share

check if you are importing FormsModule. There is no ngControl in the new forms of angular version 2 release. You should change your template 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> 
+5
Oct 25 '16 at 20:03
source share

Two things you must take care ..

  1. If you are using a submodule, you must import the FormModule into this submodule.

      **imports:[CommonModule,HttpModule,FormsModule]** 
  2. You must export a FormModule to a module

      **exports:[FormsModule],** 

    so together it looks like an import: [CommonModule, HttpModule, FormsModule], export: [FormsModule],

  3. at the top you should import FormsModule

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




if you use only app.module.ts then

no need to export .. only import required

+2
Jan 30 '18 at 11:07
source share

I ran into this problem, but none of the answers here worked for me. I googled and found that FormsModule not shared with Feature Modules

So, if your form is in a function module, you need to import and add FromsModule there.

Please write: https://github.com/angular/angular/issues/11365

+1
Apr 25 '17 at 12:24
source share

In addition to importing the form module into the ts file of the login component, you must also import NgForm.

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

It solved my problem

0
Mar 21 '18 at 8:08
source share

You must import the FormsModule and then put it in the import section.

 import { FormsModule } from '@angular/forms'; 
0
Mar 18 '19 at 0:33
source share

Simply, if you do not have an import module, import and declare import {FormsModule} from '@ angular / forms';

and if you did, then you just need to remove formControlName = 'what' from the input fields.

0
May 20 '19 at 9:54 am
source share

You must terminate the application using Ctrl + C and restart it using NG serve

0
Jun 06 '19 at 10:45
source share

I had the same problem and I decided to update all its dependencies (package.json) with the following command npm update -D && npm update -S

As @ Günter Zöchbauer pointed out, first enable the FormsModule.

-2
Sep 24 '17 at 14:35
source share



All Articles