Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't bind to 'formGroup' since it isn't a known property of 'form'

The situation

I am trying to make what should be a very simple form in my Angular application, but no matter what, it never works.

The Angular version

Angular 2.0.0 RC5

The error

Can't bind to 'formGroup' since it isn't a known property of 'form'

Enter image description here

The code

The view

<form [formGroup]="newTaskForm" (submit)="createNewTask()">    <div class="form-group">       <label for="name">Name</label>       <input type="text" name="name" required>    </div>    <button type="submit" class="btn btn-default">Submit</button> </form> 

The controller

import { Component } from '@angular/core'; import { FormGroup, FormControl, Validators, FormBuilder }  from '@angular/forms'; import {FormsModule,ReactiveFormsModule} from '@angular/forms'; import { Task } from './task';  @Component({     selector: 'task-add',     templateUrl: 'app/task-add.component.html'  }) export class TaskAddComponent {      newTaskForm: FormGroup;      constructor(fb: FormBuilder)     {         this.newTaskForm = fb.group({             name: ["", Validators.required]         });     }      createNewTask()     {         console.log(this.newTaskForm.value)     } } 

The ngModule

import { NgModule }      from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { FormsModule }   from '@angular/forms';  import { routing }        from './app.routing'; import { AppComponent }  from './app.component'; import { TaskService } from './task.service'  @NgModule({     imports: [         BrowserModule,         routing,         FormsModule     ],     declarations: [ AppComponent ],     providers: [         TaskService     ],     bootstrap: [ AppComponent ] }) export class AppModule { } 

The question

Why am I getting that error? Am I missing something?

like image 955
FrancescoMussi Avatar asked Aug 25 '16 18:08

FrancescoMussi


People also ask

Can't bind to FormGroup since it isn't a known property?

What Does This Error Mean? Angular is trying to tell us that it doesn't know about the formGroup directive on the <form> element in your component. This usually happens when the wrong forms module is imported, the right module is imported in the wrong place or the ReactiveFormsModule is just not imported at all.

Can't bind to since it isn't a known property?

What does "Can't bind to 'x' since it isn't a known property of 'y'" mean? link. This error often means that you haven't declared the directive "x" or haven't imported the NgModule to which "x" belongs. Perhaps you declared "x" in an application sub-module but forgot to export it.

Can't bind to ngModelOptions since it isn't a known property of?

Answers related to “can't bind to 'ngmodeloptions' since it isn't a known property of 'input'” If ngModel is used within a form tag, either the name attribute must be set or the form control must be defined as 'standalone' in ngModelOptions.

What is ReactiveFormsModule in Angular?

A directive which installs the MinValidator for any formControlName , formControl , or control with ngModel that also has a min attribute. NgControlStatus. Directive automatically applied to Angular form controls that sets CSS classes based on control status. NgControlStatusGroup.


2 Answers

RC6/RC7/Final release FIX

To fix this error, you just need to import ReactiveFormsModule from @angular/forms in your module. Here's the example of a basic module with ReactiveFormsModule import:

import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { FormsModule, ReactiveFormsModule } from '@angular/forms'; import { AppComponent }  from './app.component';  @NgModule({     imports: [         BrowserModule,         FormsModule,         ReactiveFormsModule     ],     declarations: [         AppComponent     ],     bootstrap: [AppComponent] })  export class AppModule { } 

To explain further, formGroup is a selector for directive named FormGroupDirective that is a part of ReactiveFormsModule, hence the need to import it. It is used to bind an existing FormGroup to a DOM element. You can read more about it on Angular's official docs page.

RC5 FIX

You need to import { REACTIVE_FORM_DIRECTIVES } from '@angular/forms' in your controller and add it to directives in @Component. That will fix the problem.

After you fix that, you will probably get another error because you didn't add formControlName="name" to your input in form.

like image 76
Stefan Svrkota Avatar answered Oct 05 '22 07:10

Stefan Svrkota


Angular 4 in combination with feature modules (if you are for instance using a shared-module) requires you to also export the ReactiveFormsModule to work.

import { NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; import { FormsModule, ReactiveFormsModule } from '@angular/forms';  @NgModule({   imports:      [     CommonModule,     ReactiveFormsModule   ],   declarations: [],   exports: [     CommonModule,     FormsModule,     ReactiveFormsModule   ] }) export class SharedModule { }  
like image 32
Undrium Avatar answered Oct 05 '22 08:10

Undrium