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

FrancescoMussi :

THE SITUATION:

Please help! I am trying to make what should be a very simple form in my Angular2 app but no matter what it never works.

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?

Stefan Svrkota :

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.

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

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

Can't bind to 'FormGroup' since it isn't a known property of 'form'. ("

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

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

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

Angular 6: Can't bind to 'formGroup' since it isn't a known property of 'form'?

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

Angular 7 testing: "Can't bind to formGroup since it isn't a known property of form"

Angular 10 - can't bind to 'formGroup' since it isn't a known property of 'form'

Angular 16 Standalone component, "Can't bind to 'formGroup' since it isn't a known property of 'form'"

Can't bind to 'formGroup' since it isn't a known property of 'form'. ReactiveFormsModule already imported

Can't bind to 'formGroup' since it isn't a known property of 'form'. error in ionic v5.2.2

Template parse errors: Can't bind to 'formGroup' since it isn't a known property of 'form'

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

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

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

error NG8002: Can't bind to 'formGroup' since it isn't a known property of 'form'. In Angular 9

Angular error message: Can't bind to 'formGroup' since it isn't a known property of 'form'

Angular Ivy: Can't bind to 'formGroup' since it isn't a known property of 'form'. However ReactiveFormsModule and FormsModule is imported

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

Angular unit test error - Can't bind to 'formGroup' since it isn't a known property of 'form'

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

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

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

Angular 16 - can't bind to 'formGroup' since it isn't a known property of 'form'

Angular 15+ - Can't bind to 'formGroup' since it isn't a known property of 'form' even after adding in modules

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

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

Can't bind to 'startingCategory' since it isn't a known property of 'div'