I am new in using Angular 4. I am trying to practice model driven forms in angular 4, But it keep getting this error.
Template parse errors: Can't bind to 'formGroup' since it isn't a known property of 'form'. (" ][formGroup] = "form" (ngSubmit)="onSubmit(form.value)"> "): ng:///ComponentsModule/AdsComponent.html@71:38 Error: Template parse errors: Can't bind to 'formGroup' since it isn't a known property of 'form'. (" ][formGroup] = "form" (ngSubmit)="onSubmit(form.value)">
I tried searching it for the issue on how to solve. Mostly the solution is by importing ReactiveFormsModule in the module. Here is the code in my component.ts
import { Component, Input, OnChanges, SimpleChange } from '@angular/core';
import { FormGroup, FormControl, ReactiveFormsModule } from '@angular/forms';
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
@Component({
templateUrl: 'ads.component.html'
})
export class AdsComponent {
form;
ngOnInit() {
this.form = new FormGroup({
ads_name: new FormControl("Hello Ads!")
});
}
constructor(
private http: HttpClient
) {
}
onSubmit = function(user) {
console.log(user);
}
}
and here is the code in my component.html
<form class="form-horizontal" [formGroup] = "form" (ngSubmit)="onSubmit(form.value)">
<div class="form-group row">
<label class="col-md-3 form-control-label" for="ads_name">Ads Name</label>
<div class="col-md-9">
<input type="text" id="ads_name" name="ads_name" class="form-control" placeholder="Ads Name" formControlName="ads_name" ngModel>
</div>
</div>
</form>
and here is the code in my module
import { ReactiveFormsModule, FormsModule } from '@angular/forms';
@NgModule({
imports: [
BrowserModule,
AppRoutingModule,
BsDropdownModule.forRoot(),
TabsModule.forRoot(),
ChartsModule,
ReactiveFormsModule,
HttpClientModule,
FormsModule
],
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.
In order to solve can't bind to 'formgroup' since it isn't a known property of 'form' error you need to import ReactiveFormsModule in each submodule file.
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.
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.
As your error states:
Template parse errors: Can't bind to 'formGroup' since it isn't a known property of 'form'. (" ][formGroup] = "form" (ngSubmit)="onSubmit(form.value)"> "): ng:///ComponentsModule/AdsComponent.html@
we can assume that your AdsComponent
is a part of ComponentsModule
declaration but you have imported ReactiveFormsModule
in AppModule
. So in order angular will be able to compile template of AdsComponent
you should:
1) either import ReactiveFormsModule
in ComponentsModule
:
@NgModule({
imports: [
...
ReactiveFormsModule
]
})
export class ComponentsModule {}
2) or import module that is exporting ReactiveFormModule
@NgModule({
exports: [
...
ReactiveFormsModule
]
})
export class SharedModule {}
@NgModule({
imports: [
...
SharedModule
]
})
export class ComponentsModule {}
See also:
Angular 2 Use component from another module
Please declare the ReactiveFormsModule as part of your testing module.
beforeEach(() => {
TestBed.configureTestingModule({
imports: [ReactiveFormsModule],
declarations: [YourComponent, ...],
providers: [...]
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With