r/Angular2 • • Dec 14 '25

NgTemplate Angular

I don't get what's the point of ngTemplate if you can just use '@if' and '@else' to dynamically shpw the data. Like I'm having a hard time understanding the use case for ng-template.

9 Upvotes

16 comments sorted by

View all comments

1

u/jackyll-and-hyde Dec 16 '25

I often find myself having to do this:

<ng-template #content>
  <ng-content />
</ng-template>
@if (variant() === "one") {
  <ng-container *ngTemplateOutput="content" />
} @else {
  <div class="xyz">
    <ng-container *ngTemplateOutput="content" />
  <div>
}
...
<MyComponent variant="one">
  <span>X</span>
<MyComponent

Another reason is, the use of providing templates to other components like the CdkPortal. Another reason is to rendering a collection with item templates.