I'm learning Angular and I decided to create separate generic elements in addition to the main project (in a library) and then import and use them according to my needs.
My problem is due to the fact that I can't import the generic components now... (I can import the Module from the library, but when I try to use my component's selector, Angular warns that the element is unknown to it and doesn't compile the server ).
Generic Component:
(.html)
<p>f-base-screen works!</p>
(.ts)
import { Component, OnInit } from '#angular/core';
#Component({
selector: 'nx-flow-base-screen',
templateUrl: './f-base-screen.component.html',
styleUrls: ['./f-base-screen.component.scss'],
})
export class FBaseScreenComponent implements OnInit {
constructor() {}
ngOnInit(): void {}
}
Library Module:
import { NgModule } from '#angular/core';
import { CommonModule } from '#angular/common';
import { FBaseScreenComponent } from './f-base-screen/f-base-screen.component';
#NgModule({
imports: [
CommonModule
],
declarations: [
FBaseScreenComponent
],
exports: [
FBaseScreenComponent
],
})
export class FLibrariesModule {}
f-libraries.ts (exports the module and its components):
export * from './f-libraries.module';
export * from './f-base-screen/f-base-screen.component';
My SharedModule (it's later imported into my app.module):
import { CommonModule } from "#angular/common";
import { NgModule } from "#angular/core";
import { FLibrariesModule } from "#nx-flow/f-libraries";
#NgModule({
imports: [CommonModule, FLibrariesModule],
exports: [CommonModule, FLibrariesModule],
providers: []
})
export class SharedModule {}
App.module.ts:
import { NgModule } from '#angular/core';
import { HttpClientModule } from '#angular/common/http';
import { BrowserModule } from '#angular/platform-browser';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { FRestService } from './shared/utils/rest.service';
import { SharedModule } from './shared/shared.module';
#NgModule({
declarations: [AppComponent],
imports: [BrowserModule, AppRoutingModule, HttpClientModule, SharedModule],
exports: [],
providers: [FRestService],
bootstrap: [AppComponent],
})
export class AppModule {}
My Home.component.module.ts (which imports the SharedModule to use the nx-flow-base-screen component):
import { NgModule } from '#angular/core';
import { WelcomeService } from '../../shared/services/flow-services/welcome.service';
import { SharedModule } from '../../shared/shared.module';
import { FRestService } from '../../shared/utils/rest.service';
import { HomeComponent } from './home.component';
#NgModule({
declarations: [HomeComponent],
imports: [SharedModule],
providers: [WelcomeService, FRestService],
bootstrap: [HomeComponent],
})
export class HomeComponentModule {}
My Home.component.html (using the component, the error that appears in the image is the same as angular throws when I compile):
<div>
<span>Home Page</span>
</div>
<nx-flow-base-screen></nx-flow-base-screen>
I already researched and followed tutorials, none of them worked so far. Are there any changes I still need to make to be able to use my component, from the library, within my main project?
If any more files are needed to reach a conclusion, I'll post a print of it.
Error:
apps/flow/src/app/views/home/home.component.html:4:3 - error
NG8001: 'nx-flow-base-screen' is not a known element:
If 'nx-flow-base-screen' is an Angular component, then verify that it is part of this module.
If 'nx-flow-base-screen' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '#NgModule.schemas' of this component
to suppress this message.
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'
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?
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.
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 { }
Ok after some digging I found a solution for "Can't bind to 'formGroup' since it isn't a known property of 'form'."
For my case, I've been using multiple modules files, i added ReactiveFormsModule in app.module.ts
import { FormsModule, ReactiveFormsModule } from '#angular/forms';`
#NgModule({
declarations: [
AppComponent,
]
imports: [
FormsModule,
ReactiveFormsModule,
AuthorModule,
],
...
But this wasn't working when I use a [formGroup] directive from a component added in another module, e.g. using [formGroup] in author.component.ts which is subscribed in author.module.ts file:
import { NgModule } from '#angular/core';
import { CommonModule } from '#angular/common';
import { AuthorComponent } from './author.component';
#NgModule({
imports: [
CommonModule,
],
declarations: [
AuthorComponent,
],
providers: [...]
})
export class AuthorModule {}
I thought if i added ReactiveFormsModule in app.module.ts, by default ReactiveFormsModule would be inherited by all its children modules like author.module in this case... (wrong!).
I needed to import ReactiveFormsModule in author.module.ts in order to make all directives to work:
...
import { FormsModule, ReactiveFormsModule } from '#angular/forms';
...
#NgModule({
imports: [
...,
FormsModule, //added here too
ReactiveFormsModule //added here too
],
declarations: [...],
providers: [...]
})
export class AuthorModule {}
So, if you are using submodules, make sure to import ReactiveFormsModule in each submodule file.
I have encountered this error during unit testing of a component (only during testing, within application it worked normally). The solution is to import ReactiveFormsModule in .spec.ts file:
// Import module
import { ReactiveFormsModule } from '#angular/forms';
describe('MyComponent', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [MyComponent],
imports: [ReactiveFormsModule], // Also add it to 'imports' array
})
.compileComponents();
}));
});
go to: app.module.ts
imports:[
....
ReactiveFormsModule
]
add this: import { ReactiveFormsModule } from '#angular/forms';
it should look like that:
mport { ReactiveFormsModule } from '#angular/forms';
#NgModule({
declarations: [
],
imports: [
ReactiveFormsModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
The error says that FormGroup is not recognized in this module. So you have to import these (below) modules in every module that uses FormGroup
import { FormsModule, ReactiveFormsModule } from '#angular/forms';
Then add FormsModule and ReactiveFormsModule into your Module's imports array.
imports: [
FormsModule,
ReactiveFormsModule
],
You may be thinking that I have already added it in AppModule and it should inherit from it? But it is not. Because these modules are exporting required directives that are available only in importing modules. Read more in Sharing modules.
Other factors for these errors may be be a spelling error like below...
[FormGroup]="form" Capital F instead of small f
[formsGroup]="form" Extra s after form
The suggested answer did not work for me with Angular 4. Instead I had to use another way of attribute binding with the attr prefix:
<element [attr.attribute-to-bind]="someValue">
If you have to import two modules then add like this below
import {ReactiveFormsModule,FormsModule} from '#angular/forms';
#NgModule({
declarations: [
AppComponent,
HomeComponentComponent,
BlogComponentComponent,
ContactComponentComponent,
HeaderComponentComponent,
FooterComponentComponent,
RegisterComponent,
LoginComponent
],
imports: [
BrowserModule,
FormsModule,
HttpModule,
routes,
ReactiveFormsModule
],
providers: [],
bootstrap: [AppComponent]
})
Firstly, it is not related to Angular versions>2. Just import the following in your app.module.ts file will fix the problems.
import { FormsModule, ReactiveFormsModule } from '#angular/forms';
Then add FormsModule and ReactiveFormsModule into your imports array.
imports: [
FormsModule,
ReactiveFormsModule
],
Note: You can also import ReactiveFormsModule to a specific module instead to app.module.ts
Keep in mind that if you have defined "Feature Modules", you'll need to import in the Feature Module, even if you already imported to the AppModule. From the Angular documentation:
Modules don't inherit access to the components, directives, or pipes that are declared in other modules. What AppModule imports is irrelevant to ContactModule and vice versa. Before ContactComponent can bind with [(ngModel)], its ContactModule must import FormsModule.
https://angular.io/docs/ts/latest/guide/ngmodule.html
I had the same issue with Angular 7. Just import following in your app.module.ts file.
import { FormsModule, ReactiveFormsModule } from '#angular/forms';
Then add FormsModule and ReactiveFormsModule in to your imports array.
imports: [
FormsModule,
ReactiveFormsModule
],
This problem occurs due to missing import of FormsModule,ReactiveFormsModule .I also came with same problem.
My case was diff. as i was working with modules.So i missed above imports in my parent modules though i had imported it into child modules,it wasn't working.
Then i imported it into my parent modules as below, and it worked!
import { ReactiveFormsModule,FormsModule } from '#angular/forms';
import { AlertModule } from 'ngx-bootstrap';
#NgModule({
imports: [
CommonModule,
FormsModule,
ReactiveFormsModule,
],
declarations: [MyComponent]
})
Your Angular version is 11+, and you use VisualStudioCode?
And you have already imported FormsModule, ReactiveFormsModule and added it into your imports-section within e.g. app.module.ts (relevant module can be different, choose the right one):
// app.module.ts (excerpt)
import { FormsModule, ReactiveFormsModule } from '#angular/forms';
imports: [
...
FormsModule,
ReactiveFormsModule,
...
],
You have the right imports (sometimes there are other libs with similar names); you have defined and initialized your form in your component?
// MyWonderfulComponent (excerpt)
import { FormBuilder, FormGroup, Validators } from '#angular/forms';
export class MyWonderfulComponent implements OnInit {
form: FormGroup;
...
constructor (private fb: FormBuilder) {
this.form = this.fb.group({
// DON'T FORGET THE FORM INITIALISATION
});
}
Your Component-Template has your form:
<form [formGroup]="form" (ngSubmit)="submit()">
<!-- MY FORM CONTROLS ARE ALREADY HERE -->
</form>
And you still get the error message "...since it isn't a known property of..." ?
then just simple restart your VisualStudioCode :)
Don't be a dumb dumb like me. I was getting the same error as above, and none of the options in previous answers worked. Then I realized I capitalized 'F' in FormGroup. Doh!
Instead of:
[FormGroup]="form"
Do:
[formGroup]="form"
Simple solution:
Step 1: Import ReactiveFormModule
import {ReactiveFormsModule} from '#angular/forms';
Step 2: Add "ReactiveFormsModule" to the import section
imports: [
ReactiveFormsModule
]
Step 3: Restart the application and done
Example:
import { NgModule } from '#angular/core';
import { CommonModule } from '#angular/common';
import {ReactiveFormsModule} from '#angular/forms';
import { EscalationManagementRoutingModule } from './escalation-management-routing.module';
import { EscalationManagementRouteWrapperComponent } from './escalation-management-route-wrapper.component';
#NgModule({
declarations: [EscalationManagementRouteWrapperComponent],
imports: [
CommonModule,
EscalationManagementRoutingModule,
ReactiveFormsModule
]
})
export class EscalationManagementModule { }
I was coming across this error when trying to do e2e testing and it was driving me crazy that there were no answers to this.
IF YOU ARE DOING TESTING, find your *.specs.ts file and add :
import {ReactiveFormsModule, FormsModule} from '#angular/forms';
For people strolling these threads about this error. In my case I had a shared module where I only exported the FormsModule and ReactiveFormsModule and forgot to import it. This caused a strange error that formgroups were not working in sub components. Hope this helps people scratching their heads.
If you have this problem when you are developing a component, you should add these two modules to your closest module:
import { FormsModule, ReactiveFormsModule } from '#angular/forms';
#NgModule({
declarations: [
AppComponent
],
imports: [
// other modules
FormsModule,
ReactiveFormsModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
And if you are developing a test for your components so you should add this module to your test file like this:
import { async, ComponentFixture, TestBed } from '#angular/core/testing';
import { ContactusComponent } from './contactus.component';
import { ReactiveFormsModule } from '#angular/forms';
describe('ContactusComponent', () => {
let component: ContactusComponent;
let fixture: ComponentFixture<ContactusComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ContactusComponent],
imports:[
ReactiveFormsModule
]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(ContactusComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
A LITTLE NOTE: Be careful about loaders and minimize (Rails env.):
After seeing this error and trying every solution out there, i realised there was something wrong with my html loader.
I've set my Rails environment up to import HTML paths for my components successfully with this loader (config/loaders/html.js.):
module.exports = {
test: /\.html$/,
use: [ {
loader: 'html-loader?exportAsEs6Default',
options: {
minimize: true
}
}]
}
After some hours efforts and countless of ReactiveFormsModule imports i saw that my formGroup was small letters: formgroup.
This led me to the loader and the fact that it downcased my HTML on minimize.
After changing the options, everything worked as it should, and i could go back to crying again.
I know that this is not an answer to the question, but for future Rails visitors (and other with custom loaders) i think this could be helpfull.
Import and register ReactiveFormsModule in your app.module.ts.
import { ReactiveFormsModule } from '#angular/forms';
#NgModule({
declarations: [
AppComponent,
HighlightDirective,
TestPipeComponent,
ExpoentialStrengthPipe
],
imports: [
BrowserModule,
ReactiveFormsModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
Make sure your spelling is correct in both .ts and .html file.
xxx.ts
profileForm = new FormGroup({
firstName: new FormControl(''),
lastName: new FormControl('')
});
xxx.html file-
<form [formGroup]="profileForm">
<label>
First Name:
<input type="text" formControlName = "firstName">
</label>
<label>
Last Name:
<input type="text" formControlName = "lastName">
</label>
</form>
I was by mistake wrote [FormGroup] insted of [formGroup]. Check your spelling correctly in .html. It doesn't throw compile time error If anything wrong in .html file.
I tried almost all the solution here but my problem was a little different(stupid).
I added the component in routing module but didn't include it main module.
So make sure your component is part of the module.
Using and import REACTIVE_FORM_DIRECTIVES:
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 { }
I had the same problem. Make sure that if using submodules (for example, you not only have app.component.module.ts), but you have a separate component such as login.module.ts, that you include ReactiveFormsModule import in this login.module.ts import, for it to work. I don't even have to import ReactiveFormsModule in my app.component.module, because I'm using submodules for everything.
File login.module.ts
import { NgModule } from '#angular/core';
import { CommonModule } from '#angular/common';
import { FormsModule, ReactiveFormsModule } from '#angular/forms';
import { IonicModule } from '#ionic/angular';
import { LoginPageRoutingModule } from './login-routing.module';
import { LoginPage } from './login.page';
#NgModule({
imports: [
CommonModule,
FormsModule,
ReactiveFormsModule,
IonicModule,
LoginPageRoutingModule
],
declarations: [LoginPage]
})
export class LoginPageModule {}
The ReactiveFormsModule and FormsModule import should be added in your custom component module and also its parent component from where it is getting called.
For example, I needed to add form for my filter component. So I should add this import in my filter module and its parent page (might be list) module from where this filter button gets clicked.
Note: if you are working inside child module's component, then you just have to import ReactiveFormsModule in child module rather than parent app root module.
Import ReactiveFormsModule in the corresponding module.
If this is just a TypeScript error but everything on your form works, you may just have to restart your IDE.
When you have a formGroup in a modal (entrycomponent), then you have to import ReactiveFormsModule also in the module where the modal is instantiated.
import { FormsModule, ReactiveFormsModule } from '#angular/forms';
and add it in imports array in the app-module.ts file.
You can get this error message even if you have already imported FormsModule and ReactiveFormsModule. I moved a component (that uses the [formGroup] directive) from one project to another, but failed to add the component to the declarations array in the new module. That resulted in the Can't bind to 'formGroup' since it isn't a known property of 'form' error message.
Hi I am trying this for last two days using http reading local json file ,not sure where the problem is
my service file
import {Injectable} from '#angular/core';
import { Http,Response } from '#angular/http';
import { Observable } from 'rxjs/Observable'
import 'rxjs/add/operator/map'
import 'rxjs/add/operator/catch'
export class AcquirerService{
private _url : string = "agent_output1.json";
constructor(private _http : Http){}
getAcquirer(){
return this._http.get(this._url).subscribe(
(data) => console.log(data)
)
// .map((response:Response) => response.json());
}
my component file
import { Component ,OnInit} from '#angular/core';
import {AcquirerService} from '../services/acquirer.service'
#Component({
selector : 'acquirer-message',
templateUrl: '../templates/acquirer.component.html',
providers :[AcquirerService]
})
export class AcquirerComponent implements OnInit{
acquirer_response ;
constructor(private _acquirerService : AcquirerService){
}
ngOnInit(){
this._acquirerService.getAcquirer();
// .subscribe(resAcquirerData => this.acquirer_response = resAcquirerData);
}
}
The error which i am getting is
metadata_resolver.js:972 Uncaught
SyntaxError {__zone_symbol__error: Error: Can't resolve all parameters for AcquirerService: (?)
my main.ts file
import { platformBrowserDynamic } from '#angular/platform-browser-dynamic';
import { enableProdMode } from '#angular/core';
import { environment } from './environments/environment';
import { AppModule } from './app/app.module';
import {HttpModule} from '#angular/http'
if (environment.production) {
enableProdMode();
}
platformBrowserDynamic().bootstrapModule(AppModule);
my app module file
import { BrowserModule } from '#angular/platform-browser';
import { NgModule } from '#angular/core';
import { FormsModule } from '#angular/forms';
import { HttpModule,JsonpModule } from '#angular/http';
import { AppComponent } from './components/app.component';
import {AcquirerComponent} from "./components/acquirer.component";
import {IssuerComponent} from "./components/issuer.component";
#NgModule({
declarations: [
AppComponent,AcquirerComponent,IssuerComponent
],
imports: [
BrowserModule,
FormsModule,
HttpModule,
JsonpModule
],
providers: [],
bootstrap: [AppComponent,HttpModule]
})
export class AppModule { }
any help will be much appreciated I am new to angular
Add AcquirerService into providers
#NgModule({
declarations: [
AppComponent,AcquirerComponent,IssuerComponent
],
imports: [
BrowserModule,
FormsModule,
HttpModule,
JsonpModule
],
providers: [AcquirerService],
bootstrap: [AppComponent,HttpModule]
})
export class AppModule { }
plus add #Injectable() before AcquirerService
#Injectable()
export class AcquirerService{ ... }
In your service file, you imported the Injectable but didn't use it. Put #Injectable() before the service name like this
#Injectable()
export class AcquirerService{
//your code here
}