【问题标题】:Angular throws error: can't bind to "ngIf" since it isn't a known property of "ng-container"Angular 抛出错误:无法绑定到“ngIf”,因为它不是“ng-container”的已知属性
【发布时间】:2020-12-30 12:59:21
【问题描述】:

我正在将项目从 Angular 7 迁移到 11,并且某个特定模块呈现此错误:

Can't bind to "ngIf" since it isn't a known property of "ng-container"

我为模块导入了CommonModule,并且之前的模块与 Angular 7 一起使用。

为了加载我使用路由的组件,它似乎工作正常,因为 ngOnInit 基于路由器 :id 执行 API 调用。

有什么想法吗?

UPD 我没有指定导致错误的组件已加载,因为我在父模块中直接路由 {path: 'some-path', component: SomeComponent}。我将其更改为延迟加载方法,现在可以使用。不确定是什么导致了错误以及为什么它在第 7 版中起作用。

【问题讨论】:

  • 在此处发布您的代码
  • @Sajeetharan 我不能。请相信我的描述
  • 我假设您需要将 ng-if 替换为 *ngIf
  • 能否请您发布您的 AppModule 代码?
  • @pbachman AppModule 加载路由模块,该模块具有到我提到的组件的路由

标签: angular angular-ng-if


【解决方案1】:

您应该使用*ngIf="condition" 而不是ng-If="condition"

使用 CommonModule 导入的工作解决方案应如下所示:

<ng-container *ngIf="condtition">TEST</ng-container>

https://angular.io/api/common/NgIf

请确保 BrowserModuleBrowserAnimationsModule 只导入一次(通常在 appModule 中)。所有其他模块应导入 CommonModule

【讨论】:

  • 请发布您的模块(导入)和代码。那件作品是我项目的测试,效果很好。您的错误与 *ngIf 无关,然后出现其他问题。
【解决方案2】:

我相信你写错了。应该是 *ngIf 而不是 ng-if

【讨论】:

  • 我在重写错误时犯了一个错误。它在错误消息中的“ngIf”,在我有 *ngIf 的代码中
【解决方案3】:

确保您导入了通用模块

import { CommonModule } from '@angular/common';

@NgModule({
    imports: [
        CommonModule
    ]
})

在 Angular 中,您需要使用 *ngIf 而不是 ng-if(它适用于 angularjs)

【讨论】:

  • 他已经提到 CommonModule 是导入的。
猜你喜欢
  • 1970-01-01
  • 2021-06-16
  • 2021-03-20
  • 2021-10-24
  • 1970-01-01
  • 2017-03-18
  • 1970-01-01
  • 1970-01-01
  • 2020-12-28
相关资源
最近更新 更多