Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

angular

I'm new in angular 2 and i try to make a reactive form but i have some trouble. After many search in stack i found no solutions.

Here you can see my error

enter image description here

The code :

View :

    <main>         <div class="container">             <h2>User data</h2>             <form [formGroup]="userForm">                 <div class="form-group">                     <label>name</label>                     <input type="text" class="form-control" formControlName="name">                 </div>                 <div class="form-group">                     <label>email</label>                     <input type="text" class="form-control" formControlName="email">                 </div>                 <div class="" formGroupName="adresse">                     <div class="form-group">                         <label>Rue</label>                         <input type="text" class="form-control" formControlName="rue">                     </div>                     <div class="form-group">                         <label>Ville</label>                         <input type="text" class="form-control" formControlName="ville">                     </div>                     <div class="form-group">                         <label>Cp</label>                         <input type="text" class="form-control" formControlName="cp">                     </div>                 </div>                 <button type="submit" class="btn btn-primary">Submit</button>             </form>         </div>     </main> 

My module.ts

    import { NgModule }      from '@angular/core';     import { CommonModule }  from '@angular/common';     import { BrowserModule } from '@angular/platform-browser';     import { ContactComponent } from './contact.component';     import { FormGroup , FormControl , ReactiveFormsModule , FormsModule } from '@angular/forms';               @NgModule({       imports: [         NgModule,         BrowserModule,         FormsModule,         ReactiveFormsModule,         FormGroup,         FormControl       ],       declarations: [         ContactComponent       ]     })     export class ContactModule {}    And my component.ts      import {Component} from '@angular/core';     import { FormGroup , FormControl } from '@angular/forms';          @Component({       selector: 'contact',       templateUrl: './contact.component.html'       // styleUrls: ['../../app.component.css']     })     export class ContactComponent {              userForm = new FormGroup({             name: new FormControl(),             email: new FormControl(),             adresse: new FormGroup({                 rue: new FormControl(),                 ville: new FormControl(),                 cp: new FormControl(),             })         });     } 

I don't understand my error because import of ReactiveForm is here. So ... i need your help :) Thanks

After I read your answer and refactoring my code, it's ok, that works! The problem was i import reactive module in the module of my page contact and i need import this in module of my app. So thanks a lot for your interest :)

Hope this answer help another people guys.

like image 703
Cyril Avatar asked Apr 06 '17 07:04

Cyril


People also ask

How do you resolve can't bind to FormGroup since it isn't a known property of form?

How Can You Fix It? You can even import both the ReactiveFormsModule and FormsModule into your app if you need to support both Template Forms and Reactive Forms in the same Angular application or feature. Once you've updated your module file to include the ReactiveFormsModule , your app and new form should work.

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

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 we use formControlName without FormGroup?

Without a parent FormGroup, [formControl]="name" worked earlier because that directive can stand alone, that is, it works without being in a FormGroup. With a parent FormGroup, the name input needs the syntax formControlName=name in order to be associated with the correct FormControl in the class.

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

I think that this is an old error that you tried to fix by importing random things in your module and now the code does not compile anymore. while you don't pay attention to the shell output, the browser reload, and you still get the same error.

Your module should be :

@NgModule({   imports: [     CommonModule,     FormsModule,     ReactiveFormsModule   ],   declarations: [     ContactComponent   ] }) export class ContactModule {} 
like image 69
n00dl3 Avatar answered Sep 28 '22 01:09

n00dl3


try with

<form formGroup="userForm">

instead of

<form [formGroup]="userForm">

like image 20
Lleopardi Avatar answered Sep 28 '22 01:09

Lleopardi