【问题标题】:Angular Template reference throwing error角度模板参考抛出错误
【发布时间】:2020-02-17 06:25:06
【问题描述】:

我有两个数组变量,需要渲染相同的 html 内容。

notification = [...]
disruptionNotification = [...]

下面是我的 HTML,

  <ng-container *ngFor="let notification of notifications; let i = index">
      <ng-container *ngTemplateOutlet="notification"></ng-container>
  </ng-container>
  <ng-container *ngFor="let notification of disruptionNotification; let i = index">
      <ng-container *ngTemplateOutlet="notification"></ng-container>
  </ng-container>

  <ng-template #notification>
      <tag-notification [type]="notification.type">
      <ng-container *ngIf="notification.titleLink && notification.title; else bwcNotificationTitleOnly">
       ....
      </ng-container>
    </tag-notification>
  </ng-template>

但是渲染后,我是这个错误TypeError: templateRef.createEmbeddedView is not a function

StackBlitz 链接:https://stackblitz.com/edit/angular-bxcmrp

预期的结果应该是,

This is Title
This is Title
This is disruptionNotifications
This is disruptionNotifications

【问题讨论】:

  • 你能创建一个重现问题的小提琴吗?
  • 在我的情况下,错误是由于我在 ngTemplateOutlet 之前忘记了 *(星号)

标签: javascript angular typescript


【解决方案1】:

您在 *ngIf else 子句中引用的任何元素都不能是任意组件,但它必须是 ng-template。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-18
    • 2017-01-13
    • 2020-06-04
    • 1970-01-01
    • 2016-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多