SB-Admin-BS4-Angular-8 icon indicating copy to clipboard operation
SB-Admin-BS4-Angular-8 copied to clipboard

When add ngForm getting error `no directive with "exportAs" set to "ngForm"`

Open bharatpatil21 opened this issue 7 years ago • 47 comments

Error: Template parse errors: There is no directive with "exportAs" set to "ngForm" ("v class="col-lg-12"> <form name="projectForm" (ngSubmit)="f.form.valid && addProject()" [ERROR ->]#f="ngForm">

Also added import { FormsModule, ReactiveFormsModule } from '@angular/forms'; in app.module

bharatpatil21 avatar Oct 25 '17 09:10 bharatpatil21

Facing Same issue.

ra11 avatar Feb 09 '18 08:02 ra11

Same issue.

ngvanmy avatar Mar 03 '18 02:03 ngvanmy

Any solution?

xiankaylle019 avatar Apr 02 '18 02:04 xiankaylle019

Make sure that you import FormsModule in app.module.ts

like that

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

and add it to imports like that :-

imports: [ BrowserModule, FormsModule,
],

that is worked for me

mahmoudsror avatar Apr 04 '18 11:04 mahmoudsror

I did what @mahmoudsror says above (and found the same lots of other places) and it didn't work for me. Also imported at the form component level too... still no luck.

goneos avatar May 02 '18 01:05 goneos

Make sure below things:

  1. import FormsModule as mahmoudsror says in app.module.ts import {FormsModule} from '@angular/forms'; imports: [ BrowserModule, FormsModule, ],

  2. in your html for make sure the #projectForm='ngForm' and same name in button

<form (ngSubmit)="onSubmit()" #projectForm="ngForm" class="form-horizontal">
. . .
. . .
. . .
<button type="submit" class="btn btn-success" [disabled]="!projectForm.form.valid">
        <i class="fa fa-floppy-o" aria-hidden="true"></i> Save
    </button>

SudhirSahoo avatar May 04 '18 14:05 SudhirSahoo

No proper solution for this error! I haven't found it anywhere.

vishwas097 avatar Jul 19 '19 10:07 vishwas097

I'm facing the same issue, all of the imports are fine, but still don't know what is the reason ...

ethyaan avatar Jul 28 '19 06:07 ethyaan

Thank You

nagarajussg avatar Aug 02 '19 09:08 nagarajussg

I had <form #regform="ngform"> after removing the "#" the error went away.

this worked for me: <form regform="ngform">

Vilaggio avatar Aug 04 '19 14:08 Vilaggio

I was using two modules, and importing ReactiveFormsModule in the right module did it for me... :D

dixitomkar1809 avatar Aug 15 '19 15:08 dixitomkar1809

I had the same issue with after import the form module with this <form #form="ngform" autocomplete="off"> but changing it to this has worked <form #form="ngForm" autocomplete="off"> so changing the 'ngform' to 'ngForm' worked. Simple mistake but hard to catch.

mohammed7690 avatar Sep 21 '19 12:09 mohammed7690

I am facing the same issue and tried all above mentioned solutions and none of them worked for me.

Anwar-Ul-Haq avatar Feb 10 '20 09:02 Anwar-Ul-Haq

I've updated to angular 9 and now I'm facing the same issue

mariocsantos avatar Feb 12 '20 16:02 mariocsantos

Same problem in Angular 9

form class="form" [formGroup]="postForm" #formDirective="ngForm" (ngSubmit)="onSubmit(postForm.value,formDirective)"

vamseeds avatar Feb 13 '20 17:02 vamseeds

Updated to Angular 9.0.1 and it works fine

mariocsantos avatar Feb 13 '20 18:02 mariocsantos

Guys if anyone is defining serparate multiple modules then: import { FormsModule, ReactiveFormsModule } from '@angular/forms'; is needed for every module(wherever ngForm or Reactive forms are being used)

and then under: @NgModule({ imports: [ CommonModule, FormsModule, ReactiveFormsModule ] })

Note: If you are using any separate xyz-routing.module.ts file then you need to do above import statement in this file as well(if you are using import statement and decalaring the component in which you are using ngForm or reactive form) under xyz-routing.module.ts). Otherwise you need to import and declare that component under xyz.module.ts.

We are getting error again and again bcz we are not providing import statement for that specific module under which the component lies which we are using Reactive form or NgForm

Hope this does the trick Thanks

RaashidFuzail avatar Mar 18 '20 08:03 RaashidFuzail

if u fide the sulotion share it with us thank u <3

haguezoum avatar Mar 24 '20 04:03 haguezoum

ERROR in src/app/app.module.ts:14:5 - error NG6001: Cannot declare 'CommonModule' in an NgModule as it's not a part of the current compilation.

14 CommonModule, ~~~~~~~~~~~~

node_modules/@angular/common/common.d.ts:118:22 118 export declare class CommonModule { ~~~~~~~~~~~~ 'CommonModule' is declared here. src/app/app.module.ts:15:5 - error NG6001: Cannot declare 'FormsModule' in an NgModule as it's not a part of the current compilation.

15 FormsModule, ~~~~~~~~~~~

node_modules/@angular/forms/forms.d.ts:2748:22 2748 export declare class FormsModule { ~~~~~~~~~~~ 'FormsModule' is declared here. src/app/app.module.ts:16:5 - error NG6001: Cannot declare 'ReactiveFormsModule' in an NgModule as it's not a part of the current compilation.

16 ReactiveFormsModule ~~~~~~~~~~~~~~~~~~~

node_modules/@angular/forms/forms.d.ts:3806:22 3806 export declare class ReactiveFormsModule { ~~~~~~~~~~~~~~~~~~~ 'ReactiveFormsModule' is declared here. src/app/app.component.html:1:1 - error NG8001: 'myform' is not a known element:

  1. If 'myform' is an Angular component, then verify that it is part of this module.
  2. To allow any element add 'NO_ERRORS_SCHEMA' to the '@NgModule.schemas' of this component.

1


src/app/app.component.ts:5:16
  5   templateUrl: './app.component.html',
  7m                 ~~~~~~~~~~~~~~~~~~~~~~
  Error occurs in the template of component AppComponent.
src/app/myform/myform.component.ts:7:50 - error NG8003: No directive found with exportAs 'ngModel'.

7     <input name="first" ngModel required #first="ngModel">
                                                 ~~~~~~~
src/app/myform/myform.component.ts:6:13 - error NG8003: No directive found with exportAs 'ngForm'.

6   <form #f="ngForm" (ngSubmit)="onSubmit(f)" novalidate>
            ~~~~~~
src/app/myform/myform.component.ts:14:31 - error NG8004: No pipe found with name 'json'.

14   <p>Form value: {{ f.value | json }}</p>
                               ~~~~~

** Angular Live Development Server is listening on localhost:4200, open your browser on http://localhost:4200/ **

AnasKamali avatar Apr 11 '20 16:04 AnasKamali

I had the same issue and tried every solution above, but non of them worked for me. The reason it didn't work for me is because I forgot to add my form-component.ts to the declarations: of app.module.ts.

So in my case I have a login.component. which uses the ngForm in app.module.ts I imported login.component and added it to the declarations

import { LoginComponent } from './login/login.component';

@NgModule({
  declarations: [
    LoginComponent
  ],

iTouchi avatar May 01 '20 12:05 iTouchi

@iTouchi That solved it for me!

GCour avatar May 07 '20 14:05 GCour

I copied some components from one project to another and i got this error even after importing all the modules,i forgot to declare the components in declarations in app.module.ts.So delclare the components!

AshishGupta11011 avatar May 19 '20 09:05 AshishGupta11011

This will also occur on an input if you've forgotten to put [(ngModel)]="someProperty" on the input itself.

rapPayne avatar Jun 26 '20 15:06 rapPayne

if nothing is working then the last option is that i have tried and it works, Change the directory of the component to nearest to the app.module.ts app -> feeds-component app.module.ts app.component.ts

i have tried everything but this was last thing that worked for me !

Samad-Ansari avatar Jul 19 '20 02:07 Samad-Ansari

Please make sure in app.module.ts import { FormsModule, ReactiveFormsModule } from '@angular/forms'; imports: [ FormsModule, ReactiveFormsModule, BrowserModule, AppRoutingModule]

It will definitely work.

In the component level ..Please use import { NgForm } from '@angular/forms'; as well.

RanjanSrivastava avatar Aug 05 '20 19:08 RanjanSrivastava

I needed to import additionally ReactiveFormsModule (in addition with FormsModule). This worked then:

<form #loginForm="ngForm" (ngSubmit)="login(loginForm.value)" autocomplete="off"> ... <input name="userName" ngModel id="userName" type="text" class="form-control" placeholder="User Name" />

This start occurring with Angular 11.

dmusic avatar Nov 28 '20 17:11 dmusic

Error: Template parse errors: There is no directive with "exportAs" set to "ngForm" ("v class="col-lg-12">

]#f="ngForm">

Also added import { FormsModule, ReactiveFormsModule } from '@angular/forms'; in app.module

This would work @ViewChild('f', { static: false }) slForm: NgForm;

Maruthi2 avatar Jan 17 '21 11:01 Maruthi2

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

ghidhaso avatar Jan 17 '21 20:01 ghidhaso

I'm facing similar issue - when building for the prod environment

ng build --prod

I'm getting a lot of parse errors

error NG8003: No directive found with exportAs 'ngForm'.

15 <form [formGroup]="loginForm" (ngSubmit)="login()" #f="ngForm">

No directive found with exportAs 'ngbDatepicker'.

46 <input class="form-control" id="inputDob" name="dob" formControlName="dob" [minDate]="{year: 1920, month: 1, day: 1}" ngbDatepicker #d2="ngbDatepicker">

Everything works fine when building for dev: ng build

When I disable AOT in the angular.json configuration section for production, it all builds fine:

          "configurations": {
            "production": {
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "extractCss": true,
              "namedChunks": false,
              "aot": false,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": false,
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ]
            }
          }

perkoren avatar Jan 19 '21 09:01 perkoren

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

Where did you get this info? Worked for me for Angular 11. I didn't realize Angular 11 changed this stuff...

jaylee79 avatar Jan 30 '21 12:01 jaylee79