Search code examples
angularangular2-template

Can't bind to 'ngForOf' since it isn't a known property of 'tr' (final release)


I'm using Angular2 2.1.0. When I want to display a list of companies, I got this error.

in file.component.ts :

public companies: any[] = [
    { "id": 0, "name": "Available" },
    { "id": 1, "name": "Ready" },
    { "id": 2, "name": "Started" }
];

In file.component.html :

<tbody>
  <tr *ngFor="let item of companies; let i =index">
     <td>{{i}}</td>
     <td>{{item.name}}</td>
  </tr>
</tbody>

Solution

  • 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 */],
      ..
    })
    

    For standalone components it is more or less the same:

    @Component({
      ...
      imports: [NgFor]
      ...
    })