'How to render ng-template from host element?

How to render only pending?

I build a layout component and I wrap each section with ng-template because I don't want to render the content immediately.

How to only render ng-template#pending?

<search [results]="searchResults$ | async" [pending]="pending$ | async">
  <form filter ...>...</form>
  <ng-template #empty>empty!</ng-template>
  <ng-template #pending>please wait..</ng-template>
  <ng-template let-context>
    <card *ngFor="let result of context.results">...</card>
  </ng-template>
</search>

I use this component like that:

 <ng-content select="[filter]"></ng-content>
 ??? <--- here, what I write here in order to render ng-template of #pending?


Solution 1:[1]

We need to use ContentChild decorator to get projected ng-template reference in the search component and then pass template reference variables to it.

search.component.ts

  @ContentChild('pending') pendingTemplateRef:TemplateRef<any>;

Then in html you have to use NgTemplateOutlet directive to render it

search.component.html

<ng-container *ngTemplateOutlet="pendingTemplateRef">
</ng-container>

Working Example

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Chellappan வ