如果模板包含*ngIf,则Angular custom structural指令无法重建ViewContainer
我对ViewContainers和Structural指令有问题 我有一个自定义结构指令=例如“permissionAccess” 它从myNGRX存储中选择数据,并查找匹配的权限。 如果没有权限,则清除ViewContainer。如果有权限,则使用注入的TemplateRef重建ViewContainer。 (这一切都很好——我已经用Dom元素、组件和视图进行了测试) 但是…如果任何Dom包含“ngIf”指令,它将无法重建ViewContainer 有人知道为什么会这样吗?我不知道 使用如果模板包含*ngIf,则Angular custom structural指令无法重建ViewContainer,angular,angular2-template,angular-directive,angular2-directives,Angular,Angular2 Template,Angular Directive,Angular2 Directives,我对ViewContainers和Structural指令有问题 我有一个自定义结构指令=例如“permissionAccess” 它从myNGRX存储中选择数据,并查找匹配的权限。 如果没有权限,则清除ViewContainer。如果有权限,则使用注入的TemplateRef重建ViewContainer。 (这一切都很好——我已经用Dom元素、组件和视图进行了测试) 但是…如果任何Dom包含“ngIf”指令,它将无法重建ViewContainer 有人知道为什么会这样吗?我不知道 使用*ng
*ngIf=“true”
模板示例工程:
<div *cwbPermission=“'ADMIN'">
<p>test container</p>
<div>
<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();
}
我已经试过了,效果很好。好的,谢谢@chellappan。我想我需要创建一个stackblitz来正确显示问题。我正在使用NGRX商店的订阅、硬编码列表和一些可能以某种方式影响重建的条件逻辑。
/**
* Creates the template content
*/
showContent(): void {
this.viewContainer.remove();
this.viewContainer.createEmbeddedView(this.templateRef, this.context, 0);
this.cdref.detectChanges();
}