17

我刚刚看到这个问题,但我仍然有同样的错误。我有一个共享模块,我将它导入到我的功能模块中。但我也尝试直接将模块导入我FormsModuleReactiveFormsModule功能模块。

Angular 2.0 最终版。

我的共享模块是:

import { CommonModule } from '@angular/common';
import { NgModule } from '@angular/core';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { UPLOAD_DIRECTIVES } from 'ng2-uploader/ng2-uploader';


import { UploadComponent } from './upload/index';

import { AuthenticationService } from './services/index';

@NgModule({
  declarations: [ UploadComponent, UPLOAD_DIRECTIVES ],
  imports: [ CommonModule ],
  providers: [ AuthenticationService ],
  exports: [
    FormsModule,
    CommonModule,
    UploadComponent,
    ReactiveFormsModule
  ]
})

export class SharedModule { }

我的功能模块:

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

import { SharedModule } from '../shared/shared.module';

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

@NgModule({
  imports: [ SharedModule ],
  declarations: [ LoginComponent ],
  exports: [ LoginComponent ]
})

export class LoginModule {
  constructor() {}
}

组件:

import { Component } from '@angular/core';
import { FormGroup, FormControl, FormBuilder, Validators } from '@angular/forms';
import { AuthenticationService } from '../shared';

@Component({
  selector: 'pol-login',
  templateUrl: 'login.component.html'
})
export class LoginComponent {
  loginForm: FormGroup;
  notValidCredentials: boolean = false;
  showUsernameHint: boolean = false;

  constructor(
    fb: FormBuilder,
    private authenticationService: AuthenticationService) {

      this.loginForm = fb.group({
        username: ['', Validators.compose([Validators.required, this.emailValidator])],
        password: ['', Validators.required]
      });
...
}

和观点:

<form class="container" (ngSubmit)="authenticate()" [ERROR ->][FormGroup]="loginForm">
....
</form>

所有路径和导入都是正确的。有任何想法吗?谢谢。

- - - [解决了] - - - -

更改[FormGroup]="loginForm"[formGroup]="loginForm":(

4

2 回答 2

18

解决方案:

import { ReactiveFormsModule } from '@angular/forms'

将此模块导入app.module.ts或您的组件类。(建议在 app.module.ts 中导入)。

然后直接...例如:---

imports: [
   ReactiveFormsModule
]
于 2016-12-20T12:53:43.103 回答
1

可能您错过了将 ReactiveFormsModule 模块导入 [yourmoduleName].module.ts

import { FormGroup, FormControl, FormBuilder, Validator, ReactiveFormsModule } from '@angular/forms';

并在下面添加导入您正在使用 formBuilder 或 formGroup 的组件

imports: [ReactiveFormsModule]

于 2017-03-21T10:34:07.077 回答