【问题标题】:can I use ng template for a separate component and use its template reference in other html?我可以将 ng 模板用于单独的组件并在其他 html 中使用其模板引用吗?
【发布时间】:2020-06-04 16:54:05
【问题描述】:

我正在尝试将 ng-template 用于作为单独组件的 html,并且我正在尝试在其他 html 中使用它的引用。这可能吗?如果是这样,我尝试了以下方法并想了解我出了什么问题?

ng-template.html

<ng-template #sample1>  
  <p>ng template content</p>
</ng-template>

显示模板.html

<div *ngIf="found;">
  <ng-container *ngTemplateOutlet="sample1"></ng-container>
</div>

DisplayTemplateComponent.ts

export class DisplayTemplateComponent   {
    @ViewChild('sample1', {static: false}) sample1: ElementRef;
    found = true;
}

【问题讨论】:

    标签: angular typescript angular8 angular-directive


    【解决方案1】:

    如果您想在应用程序中全局使用它,我认为您应该使用常规角度组件而不是 ng-template

    我不确定你为什么要这样做。无论如何,这个链接可以帮助你How to load the ng-template in separate file?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多