我使用的是Angular2 2.1.0。当我想要显示公司列表时,我得到了这个错误。

在file.component.ts中:

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

在file.component.html中:

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

当前回答

在我的例子中,问题是,我在*ngFor中使用了const而不是let。

其他回答

将BrowserModule添加到@NgModule()中的imports:[],如果它是根模块(AppModule),否则是CommonModule。

// older Angular versions
// import {BrowserModule, CommonModule} from '@angular/common';

import { BrowserModule } from '@angular/platform-browser'
..
..
@NgModule({
  imports: [BrowserModule, /* or CommonModule */],
  ..
})

我有一个问题,因为**代替*

*ngFor="let ingredient of ingredients"

**ngFor="let ingredient of ingredients"

我也有同样的问题,即使我已经在“module”中导入了“BrowserModule”和“CommonModule”。它没有工作,我的错误是,没有添加“NgModule.declarations”我的组件。

@NgModule ({
   declarations: [
     Your_Component // here
   ]
}) 

当ngFor语法没有正确编写时,这个错误也会被抛出,在我的例子中,我有:

<ng-template *ngForOf="let key of SomeObject; index as i">

它通过使用以下语法得到了修复:

<ng-template ngFor let-key [ngForOf]="SomeObject" let-i="index">

如果您正在创建自己的模块,则在自己的模块中导入CommonModule