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

当前回答

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

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

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

希望这能帮助到一些人。

其他回答

我遇到过类似的错误(*ngIf),即使我所有的导入都是OK的,并且组件呈现时没有任何其他错误+路由是OK的。

在我的例子中,AppModule没有包含这个特定的模块。奇怪的是,它并没有抱怨这一点,但这可能与Ivy使用ng serve的方式有关(类似于根据路由加载模块,但不考虑它的依赖关系)。

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

*ngFor="let ingredient of ingredients"

**ngFor="let ingredient of ingredients"

在所有代码中使用正确的语法后,请查看是否在angular模块的声明中提到了你的组件。 如下所示:

@NgModule ({ 声明:[ AppComponent, YourComponent ),

当使用"app-routing. "我们忘记导入CommonModule了。记得导入!

import { CommonModule } from "@angular/common";
@NgModule({  imports: [ CommonModule]})

如果您有模块实现,则必须导入组件并在声明中设置组件

祝你编程好运:)