本文介绍了Angular2问题:没有将"exportAs"设置为"ngForm"的指令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在面对这个令人沮丧的问题

I 'am facing this frustrating issue

这是我的 app.module.ts 所包含的内容

This is what my app.module.ts contains

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { LocationStrategy, HashLocationStrategy } from '@angular/common';
import { NgForm } from '@angular/forms';
import { FormsModule, ReactiveFormsModule }   from '@angular/forms';

import { AppComponent } from './app.component';
import { BsDropdownModule } from 'ngx-bootstrap/dropdown';
import { TabsModule } from 'ngx-bootstrap/tabs';
import { NAV_DROPDOWN_DIRECTIVES } from './shared/nav-dropdown.directive';

import { ChartsModule } from 'ng2-charts/ng2-charts';
import { SIDEBAR_TOGGLE_DIRECTIVES } from './shared/sidebar.directive';
import { AsideToggleDirective } from './shared/aside.directive';
import { BreadcrumbsComponent } from './shared/breadcrumb.component';

// Routing Module
import { AppRoutingModule } from './app.routing';

// Layouts
import { FullLayoutComponent } from './layouts/full-layout.component';
import { SimpleLayoutComponent } from './layouts/simple-layout.component';

@NgModule({
  imports: [
    BrowserModule,
    AppRoutingModule,
    BsDropdownModule.forRoot(),
    TabsModule.forRoot(),
    ChartsModule,
    FormsModule,
    ReactiveFormsModule
  ],
  declarations: [
    AppComponent,
    FullLayoutComponent,
    SimpleLayoutComponent,
    NAV_DROPDOWN_DIRECTIVES,
    BreadcrumbsComponent,
    SIDEBAR_TOGGLE_DIRECTIVES,
    AsideToggleDirective,
  ],
  providers: [{
    provide: LocationStrategy,
    useClass: HashLocationStrategy
  }],
  bootstrap: [ AppComponent ]
})
export class AppModule { }

我几乎是Angular2的新手,所以我无法弄清楚我的代码有什么问题.

I'am almost new to Angular2 so I can't figure out what is the problem with my code .

这是 equipe.component.html

This is the content of equipe.component.html

<div class="animated fadeIn">
    <div class="row">
        <div class="col-md-12">
            <div class="card">
                <div class="card-header">
                    <strong>Ajout Equipe</strong>
                </div>
                <div class="card-block">
                    <form #f="ngForm" (ngSubmit)="onSubmit(f)" class="form-horizontal">
                        <div class="form-group row">
                            <label class="col-md-3 form-control-label" for="text-input">Nom Equipe</label>
                            <div class="col-md-9">
                                <input type="text" id="text-input" name="text-input" class="form-control" placeholder="Saisir le nom de l'equipe" required>
                            </div>
                            <div class="form-group row">
                                <label class="col-md-3 form-control-label" for="textarea-input">Description</label>
                                <div class="col-md-9">
                                    <textarea id="textarea-input" name="textarea-input" rows="9" class="form-control" placeholder="Description de l'equipe"></textarea>
                                </div>
</div>
                        </div>
                    </form>
                    </div>
                    <div class="card-footer">
                        <button type="submit" class="btn btn-sm btn-primary pull-right"><i class="fa fa-dot-circle-o"></i> Ajouter</button>
                    </div>
                </div>
            </div><!--/.col-->
        </div><!--/.row-->
    </div>

这是 equipe.component.ts 的内容:

And this is the content of equipe.component.ts:

import { Component } from '@angular/core';
import { NgForm} from '@angular/forms';
import { FormsModule } from '@angular/forms';
import { BrowserModule } from '@angular/platform-browser';

@Component({
templateUrl: 'equipe.component.html',
})
export class EquipeComponent {

constructor() { }
onSubmit(f:NgForm) {
console.log('this is a test');
}
}

这是我的 ComponentsModule

This is the content of my ComponentsModule

import { NgModule } from '@angular/core';

import { ButtonsComponent } from './buttons.component';
import { CardsComponent } from './cards.component';

// Forms Component
import { FormsComponent } from './forms.component';
import { BsDropdownModule } from 'ngx-bootstrap/dropdown';

import { SocialButtonsComponent } from './social-buttons.component';
import { SwitchesComponent } from './switches.component';
import { TablesComponent } from './tables.component';

// Modal Component
import { ModalModule } from 'ngx-bootstrap/modal';
import { ModalsComponent } from './modals.component';

// Tabs Component
import { TabsModule } from 'ngx-bootstrap/tabs';
import { TabsComponent } from './tabs.component';
// Ressource Component
import { RessourceComponent } from './ressource.component';
// Equipe Component
import { EquipeComponent } from './equipe.component';
// Components Routing
import { ComponentsRoutingModule } from './components-routing.module';

@NgModule({
imports: [
ComponentsRoutingModule,
BsDropdownModule.forRoot(),
ModalModule.forRoot(),
TabsModule
],
declarations: [
ButtonsComponent,
CardsComponent,
FormsComponent,
ModalsComponent,
SocialButtonsComponent,
SwitchesComponent,
TablesComponent,
TabsComponent,
RessourceComponent,
EquipeComponent
]
})
export class ComponentsModule { }

推荐答案

必须导入FormsModule或其他将FormsModule导出到的模块@NgModule其中已声明EquipeComponent .

You have to import FormsModule or other module that exportes FormsModule to@NgModule where EquipeComponent has been declared.

@NgModule({
  imports: [
    FormsModule,
    // or SharedModule that exports FormsModule
    ...
  ],
  declarations: [
    EquipeComponent,
    SomeComponent
  ]
})
export class ComponentsModule {}

在前面的代码中,angular将使用SomeComponent指令和其他模块已导出的所有指令来编译EquipeComponent模板

In the preceding code angular will compile EquipeComponent template using SomeComponent directive and all directives that other modules has exported

export const TEMPLATE_DRIVEN_DIRECTIVES: Type<any>[] = [NgModel, NgModelGroup, NgForm];
                                                                                ^^^^^
@NgModule({
  declarations: TEMPLATE_DRIVEN_DIRECTIVES,
  providers: [RadioControlRegistry],
  exports: [InternalFormsSharedModule, TEMPLATE_DRIVEN_DIRECTIVES]
                                              ^^^^^^^^^^^^
})
export class FormsModule {
}

@Directive({
  ...
  exportAs: 'ngForm'
})
export class NgForm extends ControlContainer implements Form {

另请参见

这篇关于Angular2问题:没有将"exportAs"设置为"ngForm"的指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 01:37