【问题标题】:“Can't bind to 'ngForOf' since it isn't a known property of 'ng-container'“无法绑定到‘ngForOf’,因为它不是‘ng-container’的已知属性
【发布时间】:2021-10-24 23:25:03
【问题描述】:

我的 html 模态组件中有以下代码:

<ng-container *ngFor="let school of schools">
   {{school.name}}
<ng-container>

但是我收到以下错误:

“Can't bind to 'ngForOf' since it isn't a known property of 'ng-container'

我在网上看了其他人的解决方案,建议导入表单模块、反应表单模块和通用模块,但我已经在模态模块中导入了这些

import { FormsModule, ReactiveFormsModule } from "@angular/forms";
import { CommonModule } from "@angular/common";

有没有人对我如何摆脱这个错误有任何其他建议?

【问题讨论】:

    标签: html angular ngfor


    【解决方案1】:

    在你的 app.module.ts 中添加 BrowserModule。 Stackblitz

    import { BrowserModule } from '@angular/platform-browser';
    
    @NgModule({
      imports:[ BrowserModule],
    

    因此,您应该在 app.module 中导入 BrowserModule,而功能模块中的 CommonModule Here 是该主题的好读物

    【讨论】:

      【解决方案2】:

      ngFor 指令是从 CommonModule 导出的,尝试将相应的模块导入到您的应用模块导入或使用中的相应模块。

      【讨论】:

      • 感谢@Francisco Bernad。但是我已经在我的 modal.module 和我的 app.module 文件中导入了 CommonModule
      猜你喜欢
      • 2021-03-20
      • 2021-06-16
      • 1970-01-01
      • 2021-01-06
      • 1970-01-01
      • 1970-01-01
      • 2019-10-09
      • 2020-06-17
      • 2016-06-02
      相关资源
      最近更新 更多