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

当前回答

如果你已经采用了(BrowserModule, CommonModule, FormsModule) 它仍然没有工作

然后,您所要做的就是检查模块中是否声明了有错误的组件

其他回答

就我而言, 我已经在我的共享模块和组件将要使用的功能模块中包含了CommonModule,但在我的情况下,我在服务器启动(编译)时保存了一些文件,也许这就是为什么它没有正确地获取所包含模块的注册表

我重启了我的本地服务器,它解决了我的问题

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

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

可能有以下原因:

你的模块在imports[]中没有CommonModule 你使用*ngFor或*ngIf的组件不是任何模块的一部分。 你可能在*ngFor中有拼写错误,比如**ngFor或*ngFor等。 如果一切正常,重新启动你的应用程序,即ng serve或IDE,即VS Code, IntelliJ等。

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

*ngFor="let ingredient of ingredients"

**ngFor="let ingredient of ingredients"

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