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

库兹纳德克斯特

我对 ViewContainers 和 Structural Directives 有问题。

我有一个自定义结构指令 = 例如“permissionAccess”。

它从我的NGRX 存储中选择数据并寻找匹配的权限。如果没有权限,它会清除 ViewContainer。如果它有权限,它会使用注入的TemplateRef重建ViewContainer(这一切都很好 - 我已经用 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();
  }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章