Can't bind to FormGroup since it isn't a known property of 'form' (FormsModule, ReactiveFormsModule loaded)

Javier RB picture Javier RB · Sep 23, 2016 · Viewed 19.8k times · Source

I've just seen this question but I still have the same error. I have a shared module which I import to my feature module. But I also tried import FormsModule, ReactiveFormsModule modules to my feature module directly.

Angular 2.0 Final version.

My shared module is:

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

My feature module:

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

The component:

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]
      });
...
}

And the view:

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

All paths and imports are correct. Any ideas? Thanks.

------ [SOLVED] -------

Changed [FormGroup]="loginForm" for [formGroup]="loginForm" :(

Answer

Deepak swain picture Deepak swain · Dec 20, 2016

Solution:

import { ReactiveFormsModule } from '@angular/forms'

Import this module to app.module.ts or Your Component Class. ( Recommended to import in app.module.ts ).

Then Direct it...ex:---

imports: [
   ReactiveFormsModule
]