如果模板中包含*ngIf, Angular自定义结构指令无法重建ViewContainer



我有一个问题与viewcontainer和结构指令。

我有一个自定义的结构指令= eg " permissionAccess"

它从我的NGRX store中选择数据并查找匹配的权限。如果没有权限,它会清除ViewContainer。如果它有权限,它会重建ViewContainer使用注入的TemplateRef.(这一切都很好-我已经测试了Dom元素,组件,视图)

但是,如果任何Dom包含"ngIf">,则无法重建ViewContainer。指令。

有人知道为什么会发生这种情况吗?我不知道!*ngIf=“true”

模板示例:

<div *cwbPermission=“'ADMIN'">
<p>test container</p>
<div>
<p>Nested container1</p>
<div>
<p>Nested container2</p>
</div>
</div>
</div>

模板示例失败:

<div *cwbPermission=“'ADMIN'">
<p>test container</p>
<div *ngIf=“true">
<p>Nested container1</p>
<div>
<p>Nested container2</p>
</div>
</div>
</div>
有人能给我解释一下吗?我不知道!

所以,我不确定这是否是解决这个问题的最佳方法,但看起来它对我有用。在视图容器中创建模板后,我添加了一个changeDetectorRef.detectChanges(), UI现在按照我的预期更新。到目前为止没有发现任何问题。

/**
* Creates the template content
*/
showContent(): void {
this.viewContainer.remove();
this.viewContainer.createEmbeddedView(this.templateRef, this.context, 0);
this.cdref.detectChanges();
}

最新更新