Solution for Angular Can't bind to 'ngIf' since it isn't a known property of 'div' There are multiple ways to fix this. Incorrect ngIf syntax One way, This is due to an error caused due to misspelling capital 'I' for div. To solve this, Import CommonModule or BroswerModule or both into app.
Descriptionlink. The ngForOf directive is generally used in the shorthand form *ngFor . In this form, the template to be rendered for each iteration is the content of an anchor element containing the directive. The following example shows the shorthand syntax with some options, contained in an <li> element.
Add BrowserModule
to imports: []
in @NgModule()
if it's the root module (AppModule
), otherwise the CommonModule
.
// older Angular versions
// import {BrowserModule, CommonModule} from '@angular/common';
import { BrowserModule } from '@angular/platform-browser'
..
..
@NgModule({
imports: [BrowserModule, /* or CommonModule */],
..
})
In my case, the issue was that my teammate mentioned *ngfor
in templates instead of *ngFor
. Strange that there is no correct error to handle this issue (In Angular 4).
You have to import 'CommonModule' in the module where you are using these in-built directives like ngFor,ngIf etc.
import { CommonModule } from "@angular/common";
@NgModule({
imports: [
CommonModule
]
})
export class ProductModule { }
There can be any possible reason:
module
does not have CommonModule
in imports[]
component
, where you are using *ngFor
, is not a part of any module
.*ngFor
i.e. **ngFor
or *ngfor
etc.IDE
i.e. VS Code, IntelliJ etc.For me the problem was that I did not import the custom made module HouseModule
in my app.module.ts
. I had the other imports.
File: app.module.ts
import { HouseModule } from './Modules/house/house.module';
@NgModule({
imports: [
HouseModule
]
})
This can also happen if you don't declare a route component in your feature module. So for example:
feature.routing.module.ts:
...
{
path: '',
component: ViewComponent,
}
...
feature.module.ts:
imports: [ FeatureRoutingModule ],
declarations: [],
Notice the ViewComponent is not in the declarations array, whereas it should be.
Things to remember:
When custom modules are used (modules other than AppModule) then it is necessary to import the common module in it.
yourmodule.module.ts
import { CommonModule } from '@angular/common';
@NgModule({
imports: [
CommonModule
],
exports:[ ],
declarations: []
})
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