首页 > 解决方案 > 组件模板错误中的 ngIf 条件?

问题描述

我在组件中收到此错误BlockComponent

无法绑定到“ngIf”,因为它不是“app-adresat-list”的已知属性。

组件BlockComponent有模板:

<app-adresat-list *ngIf="element.tag === 'ADRESATS'" [parentBlock]="parentBlock" [list]="element?.children"></app-adresat-list>

为什么*ngIf对组件不起作用,它有时会发生,但并非总是如此?

我已经检查了BlockComponent在 app.module 中注册的内容。

我试图在此替换模板:

<div *ngIf="true">Done</div>

有效,为什么?

所有代码是:

<ng-container *ngFor="let element of documentBlock?.children">
    <ng-container *ngIf="element.type === fielType.Block">
        <app-block [parentBlock]="documentBlock?.children" [element]="element"></app-block>
    </ng-container>
</ng-container>

标签: angular

解决方案


请从提供组件的模块中导入 CommonModule。

import { CommonModule } from '@angular/common'; 
import { BrowserModule } from '@angular/platform-browser'; 



@NgModule(
{ imports: [CommonModule], 
declarations: [MyComponent]
 ... })

 class MyComponentModule {}

推荐阅读