“exportAs”设置为“ngForm”没有指令

时间:2018-02-26 07:50:08

标签: angular5

我已加入FormsModule,但它显示错误。

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

我的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');

    }


}

我的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 { }  

我的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>

我尝试制作新的角度项目,它适用于主app.module.ts,app.component.ts和app.component.html文件,但是当我尝试使用登录文件时,它无效。

我是Angular-5的新手,所以当我们创建新的登录模块时,我可能会遗漏一些东西。

编辑: -

应用程序组件: -

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 { }

我的登录页面是我创建的文件在view文件夹下。 来自app的路径。

视图/页/ login.component.html 视图/页/ login.component.ts 视图/页/ login.module.ts

2 个答案:

答案 0 :(得分:0)

万一它不适合某人,有一件事也需要看。当我从另一个项目复制并粘贴到我的项目中时,我遇到了同样的问题,我忘了更改按钮中的名称。

确保以下事项:

1)在app.module.ts中导入FormsModule

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

2)在你的html中确保按钮中的#projectForm ='ngForm'和同名(即projectForm)

<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>

答案 1 :(得分:0)

Angular 6/7/8 遇到相同的错误,只需删除
即可解决 heroForm="ngForm可以工作