Learn how to resolve the common Angular error "'Can't bind to 'ngForOf' since it isn't a known property of 'tr'" and get your code working again.
The error "Can't bind to 'ngForOf' since it isn't a known property of '...'" in Angular indicates that the template engine cannot find the *ngFor directive. This usually happens for a few common reasons: Make sure you have imported the necessary modules, declared your component correctly, avoided typos, and are using a supported Angular version. If you're still facing issues, provide code snippets, module structure, and your Angular version for further assistance.
The error "Can't bind to 'ngForOf' since it isn't a known property of '...'" in Angular indicates that the template engine cannot find the *ngFor directive. This usually happens for a few common reasons:
1. BrowserModule or CommonModule Not Imported
*ngFor directive is part of Angular's core directives, which are available through either BrowserModule or CommonModule.BrowserModule is intended for the root module of your application (usually AppModule).CommonModule is for all other feature modules.Example (app.module.ts):
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
@NgModule({
declarations: [AppComponent],
imports: [BrowserModule], // Make sure BrowserModule is imported
bootstrap: [AppComponent]
})
export class AppModule { }Example (feature.module.ts):
import { CommonModule } from '@angular/common';
import { NgModule } from '@angular/core';
import { FeatureComponent } from './feature.component';
@NgModule({
declarations: [FeatureComponent],
imports: [CommonModule], // Make sure CommonModule is imported
exports: [FeatureComponent]
})
export class FeatureModule { }2. Component Not Declared in a Module
*ngFor within its template, ensure it's declared in the declarations array of its corresponding module.Example:
// ... other imports ...
import { MyComponent } from './my.component';
@NgModule({
declarations: [
AppComponent,
MyComponent // Declare your component here
],
// ... other module properties ...
})
export class AppModule { }3. Typos and Case Sensitivity
*ngFor (case-sensitive).ngForOf property: It should be exactly ngForOf.Example (Correct Usage):
<ul>
<li *ngFor="let item of items">{{ item }}</li>
</ul>4. Outdated Angular Version
*ngFor might arise from using a very outdated Angular version. Ensure you're using a recent, supported version.Troubleshooting Steps:
BrowserModule in your root module and CommonModule in any feature modules where you're using *ngFor.*ngFor is declared in the declarations array of its module.ngForOf property.If you've followed these steps and are still encountering the error, provide the following information for more specific assistance:
This code snippet explains how to troubleshoot the Angular error "Can't bind to 'ngForOf' since it isn't a known property of '...'". The error happens when you use the *ngFor directive without importing CommonModule in your Angular module. The solution is to import CommonModule into the module where the component using *ngFor is declared. This makes the directive available for use in your component's template. The example provides code snippets to illustrate the error and the correction.
This example will showcase a simplified scenario where the error occurs and how to fix it.
File Structure:
angular-app/
โโโ src/
โ โโโ app/
โ โ โโโ app.module.ts
โ โ โโโ app.component.ts
โ โ โโโ items/
โ โ โโโ items.component.ts
โ โ โโโ items.component.html
โ โโโ main.ts
โโโ ...
1. The Error:
Let's say we have a component items.component.ts that tries to use *ngFor without importing CommonModule:
items.component.ts:
import { Component } from '@angular/core';
@Component({
selector: 'app-items',
templateUrl: './items.component.html'
})
export class ItemsComponent {
items = ['Item 1', 'Item 2', 'Item 3'];
}items.component.html:
<ul>
<li *ngFor="let item of items">{{ item }}</li>
</ul>items.module.ts (Incorrect):
import { NgModule } from '@angular/core';
import { ItemsComponent } from './items.component';
@NgModule({
declarations: [ItemsComponent],
exports: [ItemsComponent]
})
export class ItemsModule { }This will result in the error: "Can't bind to 'ngForOf' since it isn't a known property of 'li'."
2. The Solution:
To fix this, we need to import CommonModule into items.module.ts:
items.module.ts (Corrected):
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common'; // Import CommonModule
import { ItemsComponent } from './items.component';
@NgModule({
declarations: [ItemsComponent],
imports: [CommonModule], // Add CommonModule to imports
exports: [ItemsComponent]
})
export class ItemsModule { }3. Additional Notes:
ItemsModule is imported into your AppModule.This example demonstrates a common scenario where the error occurs. By ensuring the correct imports and declarations, you can resolve this issue and use the *ngFor directive effectively in your Angular applications.
Understanding the Root Cause:
*ngFor as a valid directive within your component's template. This is almost always related to the directive not being accessible due to module imports or component declarations.Beyond CommonModule and BrowserModule:
CommonModule and BrowserModule are the most common sources of *ngFor, remember that any module exporting directives can potentially make *ngFor available. If you have custom modules with directives, ensure they are correctly imported and exported.Angular CLI and Module Management:
BrowserModule into AppModule and generating modules for new components. However, it's still essential to understand these concepts in case you encounter issues or are working with a manually configured project.Lazy Loading and Shared Modules:
*ngFor within a lazy-loaded module, ensure CommonModule is imported into that module's imports, not just in your shared modules.Debugging Tips:
Best Practices:
Remember: This error is usually straightforward to fix once you understand the underlying cause. By following the troubleshooting steps and keeping these additional notes in mind, you can quickly resolve it and continue building your Angular applications.
This error means Angular can't find the *ngFor directive, essential for looping through data in templates. Here's a breakdown of common causes and how to fix them:
Causes:
Missing Module Imports:
BrowserModule in your root module (AppModule).CommonModule in all other feature modules using *ngFor.Component Not Declared:
*ngFor within the declarations array of its corresponding module.Typos:
*ngFor (case-sensitive).ngForOf (case-sensitive).Outdated Angular Version:
Troubleshooting Steps:
BrowserModule / CommonModule).*ngFor and ngForOf.Need More Help?
Provide these details for further assistance:
By addressing these common causes and following the troubleshooting steps, you can effectively resolve the "Can't bind to 'ngForOf'" error and unlock the power of the *ngFor directive for dynamic rendering in your Angular applications. Remember to pay close attention to module imports, component declarations, and potential typos. If you encounter persistent issues, providing specific code snippets, module structure, and your Angular version will enable others to assist you more effectively.
List below! <li *ngfor = "let avatar of avatars" > {{ avatar.total }} </li> Can you give me a information...
NG0303: Can't bind to 'ngForAs' since it isn't a known property of 'tr ... | Hello, I am having a problem on a For loop in Angular, I have this error showing NG0303: Can't bind to 'ngForAs' since it isn't a known property of 'tr'. I have imported the BrowserModule into my application, and the CommonModule on my page. I donโt understand where this problem can come from. Thankโs for help !
Angular2: Error โ Can't bind to 'ngForOf' since it isn't a known property | While displaying data from web-api in html code, I used *ngFor, which isย a repeater directive โ a way to customize data display. I came across below issue:ย EXCEPTION: Unhandled Promise rejection: โฆ
Can't bind to 'ngForOf' since it isn't a known property of 'ion-col ... | Hey there ! I am getting this error for a long time and I am tired of trying to find where isnโt the CommonModule or BrowserModule imported, as I saw in Stackoverflow and reddit to import the correct modules or even if I misspelled the *ngFor. So basically my app is based in sidemenu navigation with different pages (Home, Shops, Settings and Info) and when I try to go to the Shops page, it loads but gives me the error in this post title! I will put some screenshots of the routing modules and m...
[Erreur] Can't bind to 'ngForOf' since it isn't a known property of 'li'. | May 27, 2021 ... le CommonModule est bien dans le sous Module ??? J'avais fait un test dans le app.component d'une premiere version cela fonctionnait, et jย ...