I\'ve got the following error when launching my Angular app, even if the component is not displayed.
I have to comment out the so that my
I am using Angular 7
I have to import ReactiveFormsModule because I am using FormBuilder class to create reactive form.
import {
FormsModule,
ReactiveFormsModule } from '@angular/forms';
@NgModule({
imports: [
CommonModule,
FormsModule,
ReactiveFormsModule
], declarations: []})
This is for the folks who use plain JavaScript instead of Type Script. In addition to referencing the forms script file on top of the page like below
<script src="node_modules/@angular/forms/bundles/forms.umd.js"></script>
you should also tell the the module loader to load the ng.forms.FormsModule
. After making the changes my imports
property of NgModule
method looked like below
imports: [ng.platformBrowser.BrowserModule, ng.forms.FormsModule],
Happy coding!
In ngModule
you need to import FormsModule
, because ngModel
is coming from FormsModule
.
Please modify your app.module.ts like the below code I have shared
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
@NgModule({
declarations: [
AppComponent,
HomeComponent
],
imports: [
BrowserModule,
AppRoutingModule,
FormsModule
],
bootstrap: [AppComponent]
})
export class AppModule { }
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
FormsModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule {}
Sometimes you get this error when you try to use a component from a module, which is not shared, in a different module.
For example, you have 2 modules with module1.componentA.component.ts and module2.componentC.component.ts and you try to use the selector from module1.componentA.component.ts in a template inside module2 (e.g. <module1-componentA [someInputVariableInModule1]="variableFromHTTPRequestInModule2">
), it will throw the error that the someInputVariableInModule1 is not available inside module1.componentA.component.ts - even though you have the @Input() someInputVariableInModule1
in the module1.componentA.
If this happens, you want to share the module1.componentA to be accessible in other modules.
So if you share the module1.componentA inside a sharedModule, the module1.componentA will be usable inside other modules (outside from module1), and every module importing the sharedModule will be able to access the selector in their templates injecting the @Input()
declared variable.
You need to import FormsModule in your root module if this component is in the root i.e. app.module.ts
Kindly open app.module.ts
Import FormsModule from @angular/forms
Ex:
import { FormsModule } from '@angular/forms';
and
@NgModule({
imports: [
FormsModule
],
})