【问题标题】:There is no directive with “exportAs” set to “ngForm”没有将“exportAs”设置为“ngForm”的指令
【发布时间】:2018-08-05 15:02:24
【问题描述】:

我已包含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>

我尝试制作新的 Angular 项目,它适用于主 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 { }

我的登录页面是我创建的文件位于视图文件夹下。 来自app 的路径。

views/pages/login.component.html 视图/页面/login.component.ts 视图/页面/login.module.ts

【问题讨论】:

    标签: angular5


    【解决方案1】:

    Angular 6 / 7 / 8 有相同的错误,只需删除 #
    heroForm="ngForm 即可解决

    【讨论】:

    • 经过大量搜索后,这一款很有魅力。
    【解决方案2】:

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

    确保以下事项:

    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>
    

    【讨论】:

      【解决方案3】:

      这是 node_module 的错误。

      我刚刚更新了节点包,它正在工作。 因为@angular/core 没有正确导入。

      所以,为此我在 sublime-3 中安装了 typescript 包,安装后它显示错误,

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

      所以,在获取节点文件时出现了一些问题。

      【讨论】:

        猜你喜欢
        • 2017-11-08
        • 2017-09-10
        • 2018-05-12
        • 2018-01-16
        • 2016-12-03
        • 2018-10-20
        • 2018-11-20
        • 2017-01-26
        相关资源
        最近更新 更多