Skip to content
Related Articles

Related Articles

Angular 10 NgTemplateOutlet Directive

Improve Article
Save Article
  • Last Updated : 03 Jun, 2021
Improve Article
Save Article

In this article, we are going to see what is NgTemplateOutlet in Angular 10 and how to use it.

The NgTemplateOutlet in Angular10 is used to insert an embedded view from a prepared TemplateRef. NgTemplateOutlet adds the reference element which is displayed on the page.

Syntax:

<li *NgTemplateOutlet='condition'></li>

NgModule: Module used by NgTemplateOutlet is:

  • CommonModule

 

Selectors:

  • [NgTemplateOutlet]

Approach: 

  • Create an angular app to be used.
  • There is no need for any import for the NgIf to be used.
  • in app.component.html add NgTemplateOutlet Directive to the element with its reference.
  • Serve the angular app using ng serve to see the output.

Example 1:

app.component.html




<ng-container *ngTemplateOutlet="gfg"></ng-container>
  
<ng-template #gfg>
    <h1>GeeksforGeeks</h1>
  
    <div>
        ngTemplateOutlet Directive
    </div>
</ng-template>

Output:

Example 2:

app.component.html




<div *ngTemplateOutlet="gfg"></div>
  
<ng-template #gfg>
    <h1>
        GeeksforGeeks
    </h1>
    <div>
        We can also use ngTemplateOutlet Directive with a div 
    </div>
</ng-template>

Output:

Reference: https://angular.io/api/common/NgTemplateOutlet


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!