角度抛出错误:无法绑定到"ngIf",因为它不是"ng-container"的已知属性



我正在进行一个从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个版本中有效。

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

导入CommonModule后,工作解决方案应如下所示:

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

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

请确保BrowserModuleBrowser动画模块只导入一次(通常在appModule中(。所有其他模块都应导入CommonModule

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

确保导入了通用模块

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

在angular中,您需要使用*ngIf而不是ng-if(用于angularjs(

最新更新