我使用的是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>

当前回答

需要记住的事情:

当使用自定义模块(除AppModule之外的模块)时,需要导入其中的公共模块。

yourmodule.module.ts

import { CommonModule } from '@angular/common';

@NgModule({
  imports: [
    CommonModule
  ],
  exports:[ ],
  declarations: []
})

其他回答

我有一个问题,因为json数据中的空白。

你必须在使用ngFor、ngIf等内置指令的模块中导入CommonModule。

import { CommonModule } from '@angular/common'
       
@NgModule({
    imports: [
        CommonModule
    ]
})
    
export class ProductModule { }

未来的读者

检查以下每一项:

该组件是在SINGLE angular模块中声明的 确保你有import {CommonModule} from '@angular/common'; 重新启动IDE/编辑器 重新启动开发服务器(ng serve)

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

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

*ngFor="let ingredient of ingredients"

**ngFor="let ingredient of ingredients"