There is no directive with “exportAs” set to “ngForm”

余生颓废 提交于 2021-02-10 13:15:56

问题


I have included FormsModule but it showing me error.

There is no directive with “exportAs” set to “ngForm” 

My Login.component.ts :-

import { Component } from '@angular/core';
import { Http, Response , RequestOptions , Headers , URLSearchParams } from '@angular/http';
import {FormsModule} from '@angular/forms';

@Component({
    templateUrl: './login.component.html'
})

export class LoginComponent {
    apiRoot : String = "http://httpbin.org";
    // search code starts here

    // data = string;
    constructor(private http: Http){

    }
    onSubmit(value){

        let name = value.name;
        let password = value.password;
        let url = `${this.apiRoot}/post`;
        this.http.post(url, {name:name,password:password}).subscribe(res => console.log(res.json()));
        console.log('submit');

    }


}

My login.module.ts :-

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

import { jqxGridComponent } from 'jqwidgets-scripts/jqwidgets-ts/angular_jqxgrid';
import { AppComponent } from './../../app.component';
import { FormsModule, ReactiveFormsModule , FormGroup} from '@angular/forms';
import { HttpModule } from '@angular/http';
import { LoginComponent } from './login.component';

@NgModule({
  declarations: [
      AppComponent, jqxGridComponent, LoginComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    ReactiveFormsModule

  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class LoginModule { }  

my login.component.html

<div class="">
  <form (ngSubmit)="onSubmit(heroForm.value)" #heroForm="ngForm">
      <input type="text" name="name" [(ngModel)]="name" />
      <input type="password" name="password" [(ngModel)]="password" />
      <br/>
       <input type="checkbox" name="vehicle" value="Bike" [(ngModel)]="vehicle"> I have a bike<br>
         <input type="checkbox" name="vehicle" value="Car" > I have a car<br>
      <button type="submit">Send</button>
  </form>
</div>

I tried with making new angular project and it works with main app.module.ts, app.component.ts and app.component.html files but when I tried with login files it didn't works.

I'm new to Angular-5 so may be I'm missing something when we are creating new login modules.

Edit :-

App components :-

app.component.ts 

import { Component, OnInit } from '@angular/core';
import { Router, NavigationEnd } from '@angular/router';

@Component({
  // tslint:disable-next-line
  selector: 'body',
  template: '<router-outlet></router-outlet>'
})
export class AppComponent implements OnInit {
  constructor(private router: Router) { }

  ngOnInit() {
    this.router.events.subscribe((evt) => {
      if (!(evt instanceof NavigationEnd)) {
        return;
      }
      window.scrollTo(0, 0)
    });
  }
}

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { NgModule } from '@angular/core';
import { LocationStrategy, HashLocationStrategy } from '@angular/common';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { AppComponent } from './app.component';
// Import containers
import {
  FullLayoutComponent,
  SimpleLayoutComponent
} from './containers';

const APP_CONTAINERS = [
  FullLayoutComponent,
  SimpleLayoutComponent
]

// Import components
import {
  AppAsideComponent,
  AppBreadcrumbsComponent,
  AppFooterComponent,
  AppHeaderComponent,
  AppSidebarComponent,
  AppSidebarFooterComponent,
  AppSidebarFormComponent,
  AppSidebarHeaderComponent,
  AppSidebarMinimizerComponent,
  APP_SIDEBAR_NAV
} from './components';

const APP_COMPONENTS = [
  AppAsideComponent,
  AppBreadcrumbsComponent,
  AppFooterComponent,
  AppHeaderComponent,
  AppSidebarComponent,
  AppSidebarFooterComponent,
  AppSidebarFormComponent,
  AppSidebarHeaderComponent,
  AppSidebarMinimizerComponent,
  APP_SIDEBAR_NAV
]

// Import directives
import {
  AsideToggleDirective,
  NAV_DROPDOWN_DIRECTIVES,
  ReplaceDirective,
  SIDEBAR_TOGGLE_DIRECTIVES
} from './directives';

const APP_DIRECTIVES = [
  AsideToggleDirective,
  NAV_DROPDOWN_DIRECTIVES,
  ReplaceDirective,
  SIDEBAR_TOGGLE_DIRECTIVES
]

// Import routing module
import { AppRoutingModule } from './app.routing';

// Import 3rd party components
import { ChartsModule } from 'ng2-charts/ng2-charts';
import { BsDropdownModule } from 'ngx-bootstrap/dropdown';
import { ModalModule } from 'ngx-bootstrap';
import { TabsModule } from 'ngx-bootstrap/tabs';

@NgModule({
  imports: [
    BrowserModule,
    BrowserAnimationsModule,
    AppRoutingModule,
    ChartsModule,
    HttpModule,
    FormsModule,
    ReactiveFormsModule,
    BsDropdownModule.forRoot(),
    ModalModule.forRoot(),
    TabsModule.forRoot(),
  ],
  declarations: [
    AppComponent,
    ...APP_CONTAINERS,
    ...APP_COMPONENTS,
    ...APP_DIRECTIVES
  ],
  providers: [{
    provide: LocationStrategy,
    useClass: HashLocationStrategy
  }],
  bootstrap: [ AppComponent ]
})
export class AppModule { }

My Login Page is which I have created files is under view folder. Path from app.

views/pages/login.component.html views/pages/login.component.ts views/pages/login.module.ts


回答1:


having same error with Angular 6 / 7 / 8 and resolved just by removing #
heroForm="ngForm will work




回答2:


It was node_module's error.

I have just updated the node packages and it's working. Because @angular/core was not importing properly.

So, for that I have install typescript package in sublime-3 and after install it was showing error in,

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

So, It was some problem while fetching node files.




回答3:


Just in case it does not work for someone, one thing also need to see. I was having the same problem when I did copy from another project and paste in my project I forgot to change the name in button.

Make sure below things:

1) import FormsModule in app.module.ts

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

2) in your html for make sure the #projectForm='ngForm' and same name (i.e. projectForm) 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>


来源:https://stackoverflow.com/questions/48983713/there-is-no-directive-with-exportas-set-to-ngform

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!