我使用的是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语法没有正确编写时,这个错误也会被抛出,在我的例子中,我有:

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

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

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

其他回答

所以请确保

指令中没有语法错误 浏览器(在应用程序模块)和公共(在其他/子)模块被导入(与上面提到的Günter Zöchbauer相同) 如果你在应用程序中有路由,那么路由模块应该导入到应用程序模块中 所有路由组件的模块也被导入到App模块中,例如: app-routing.module。Ts为: const routes: routes = [ {path: ",组件:CustomerComponent}, {path: 'admin',组件:AdminComponent} ];

然后App模块必须导入@NgModule()中的CustomerComponent和AdminComponent模块。

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

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

*ngFor="let ingredient of ingredients"

**ngFor="let ingredient of ingredients"

如果您没有在特性模块中声明路由组件,也会发生这种情况。例如:

feature.routing.module.ts:

...
    {
        path: '',
        component: ViewComponent,
    }
...

feature.module.ts:

     imports: [ FeatureRoutingModule ],
     declarations: [],

注意ViewComponent不在declarations数组中,而它应该在。

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

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