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

当前回答

对我来说,问题是我没有在我的app.module.ts中导入定制的模块HouseModule。我有其他的进口。

文件:app.module.ts

import { HouseModule } from './Modules/house/house.module';

@NgModule({
  imports: [
    HouseModule
  ]
})

其他回答

可能有以下原因:

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

以我为例,问题是我的队友在模板中提到了*ngfor而不是*ngfor。奇怪的是,没有正确的错误来处理这个问题(在Angular 4中)。

对于Angular 10:

将BrowserModule添加到routes模块的导入中。 确保你在app模块声明中添加了不工作的组件。

如果不执行第2步,将触发此错误!

请务必重新启动ng服务!!

将BrowserModule添加到@NgModule()中的imports:[],如果它是根模块(AppModule),否则是CommonModule。

// older Angular versions
// import {BrowserModule, CommonModule} from '@angular/common';

import { BrowserModule } from '@angular/platform-browser'
..
..
@NgModule({
  imports: [BrowserModule, /* or CommonModule */],
  ..
})

将该组件添加到app.module中

import { ModalComponent } from './modal/modal.component';

@NgModule({
  declarations: [ModalComponent],
  entryComponents: [ModalComponent],
  
  }),
  providers: [
  ],
  bootstrap: [AppComponent]
})