我使用的是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="let ingredient of ingredients"

**ngFor="let ingredient of ingredients"

其他回答

我已经在mycomponent.module中导入了CommonModule。和app.module中的BrowserModule。ts,将MyComponent添加到@ngModule的声明中,并重置vscode,就像前面提到的所有解决方案一样。

最终解决了这个问题,我没有通过获取请求迭代接收到的数据,而是使用与ElementModel匹配的硬编码数据模拟elements数组。它工作没有问题。

意识到我的ElementModel接口与我从取回请求接收到的数据不匹配。

希望这能帮助到一些人。

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

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

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

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

需要记住的事情:

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

yourmodule.module.ts

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

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

为了防止有人在尝试导入CommonModule后仍然遇到错误,请尝试重新启动服务器。它竟然有效

在我的例子中,这个组件没有在app.module.ts中列出。