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

其他回答

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

feature.routing.module.ts:

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

feature.module.ts:

     imports: [ FeatureRoutingModule ],
     declarations: [],

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

我有同样的错误,但我有CommonModule导入。相反,我在不应该的地方留下了逗号,因为在分割模块时需要复制/粘贴:

@NgModule({
    declarations: [
        ShopComponent,
        ShoppingEditComponent
    ],
    imports: [
        CommonModule,
        FormsModule,
        RouterModule.forChild([
            { path: 'shop', component: ShopComponent }, <--- offensive comma
        ])
    ]
})

我收到了错误,因为我正在使用的组件没有在模块的declarations:[]部分中注册。

添加组件后,错误消失了。我本希望看到比这条错误消息更清晰的信息来指出真正的问题。

通过在其他(新/自定义)模块中导入CommonModule,许多答案似乎趋于一致。 这一步在所有情况下都是不够的。

完整的解决方案分为两步:

让NgIf, NgFor等指令对你的项目可见。 在主组件中以正确的方式重新组装所有内容

点1 主模块中的BrowserModule似乎足够访问NgFor了。 Angular文档把它写在这里:。

CommonModule用于导出所有基本的Angular指令和管道,比如NgIf、NgForOf、DecimalPipe等等。由BrowserModule重新导出,

参见此处接受的答案:CommonModule vs BrowserModule

点2 唯一需要改变的是(在我的情况下)如下:

导入模块 导入组件 Ng构建(重要!) ng服务

app.module.ts

@NgModule({
    imports: [
        BrowserModule,
        OtherModule
    ],
    declarations: [OtherComponent, AppComponent],
    bootstrap: [AppComponent]
})
export class AppModule {
}

other.html

<div *ngFor='let o of others;'> 
</div>

other.component.ts

@Component({
    selector: 'other-component',
    templateUrl: './other.html'
})
export class OtherComponent {
}

app.module.ts

@NgModule({
    imports: [],
    providers: []
})
export class OtherModule{
}

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