Angular <ng-template> Example

By Arvind Rai, October 11, 2021
This page will walk through Angular <ng-template> example. The <ng-template> is an Angular element for rendering HTML. It is not displayed directly, it can be displayed using structural directive, ViewContainerRef etc. Suppose we have following code in our HTML template.
  <p>Hello World!</p>
When the code runs then the code written inside <ng-template> will not be displayed but there will be a comment.
Before rendering HTML, angular replaces <ng-template> with a comment. <ng-template> can be used with structural directive, ViewContainerRef etc. In our example we will discuss how to use <ng-template> with ngFor, ngIf and ngSwitch. We will also create custom structural directive and we will use that with <ng-template>. Using TemplateRef and ViewContainerRef we can reuse the code of <ng-template> in our HTML template. The <ng-template> example with ngFor is as follows.
<ng-template ngFor let-person [ngForOf]= "allPersons" let-i="index">
  <p>{{i + 1}}. {{}} : {{person.age}} </p>
Now find the complete example step-by-step.

Technologies Used

Find the technologies being used in our example.
1. Angular 12.1.0
2. Node.js 12.14.1
3. NPM 7.20.3

Project Structure

Find the project structure of our demo application.
|   |
|   |--app 
|   |   |
|   |   |--app.module.ts
|   |   |--app.component.ts
|   |   |--ng-template-ngfor.component.ts
|   |   |--ng-template-ngfor.component.html
|   |   |--ng-template-ngif.component.ts
|   |   |--ng-template-ngif.component.html
|   |   |--ng-template-ngswitch.component.ts
|   |   |--ng-template-ngswitch.component.html
|   |   |--message.directive.ts
|   |   |--cp-msg.component.ts
|   |   |--cp-msg.component.html
|   |   |--cp-if.directive.ts
|   |   |--cp-if-demo.component.ts
|   |   |--cp-if-demo.component.html
|   |   
|   |--main.ts
|   |--index.html
|   |--styles.css

1. <ng-template> with ngFor

Find the example to use <ng-template> with ngFor.
import { Component } from '@angular/core';

   selector: 'app-ngfor',
   templateUrl: './ng-template-ngfor.component.html'
export class NgTemplateNgForComponent { 
   allPersons = [
     {name: 'Mahesh', age: '25'},
     {name: 'Shakti', age: '23'},
     {name: 'Krishna', age: '23'},
     {name: 'Radha', age: '21'},	 
<h3>ng-template with ngFor</h3>
<ng-template ngFor let-person [ngForOf]= "allPersons" let-i="index">
  <p>{{i + 1}}. {{}} : {{person.age}} </p>
let declares template input variable. The properties of NgFor such as index, first, last, even, odd can be assigned to a variable using let within the <ng-template>. Find the sample code.
<ng-template ngFor let-item [ngForOf]="items" let-i="index" let-o="odd" let-e="even" let-f="first" let-l="last" [ngForTrackBy]="trackByFn" >

2. <ng-template> with ngIf

Here we will provide example to use <ng-template> with ngIf, ngIf-else and ngIf-then-else.
import { Component } from '@angular/core';

   selector: 'app-ngif',
   templateUrl: './ng-template-ngif.component.html'   
export class NgTemplateNgIfComponent { 
  toggleFlag1= true;
  toggleFlag2= true;
  toggleFlag3= true;
  onToggle1() {
	  this.toggleFlag1 = (this.toggleFlag1 === true)? false : true;
  onToggle2() {
	  this.toggleFlag2 = (this.toggleFlag2 === true)? false : true;
  onToggle3() {
	  this.toggleFlag3 = (this.toggleFlag3 === true)? false : true;
<h3>ng-template with ngIf</h3>
<button type="button" (click)="onToggle1()">Toggle</button>

<ng-template [ngIf]= "toggleFlag1" >
   <div>Hello World!</div>

<h3>ng-template with ngIf-else</h3>

<button type="button" (click)="onToggle2()">Toggle</button>

<div *ngIf="toggleFlag2; else msgElseBlock" >
   <div>Hello World!</div>

<ng-template #msgElseBlock>
   <div>Else Block: Hello World! </div>

<h3>ng-template with ngIf-then-else</h3>

<button type="button" (click)="onToggle3()">Toggle</button>

<div *ngIf="toggleFlag3; then thenBlock else elseBlock">

<ng-template #thenBlock>
    <div>Then Block: Hello World!</div>
<ng-template #elseBlock>
    <div>Else Block: Hello World!</div>
In case of then and else, we need to assign a local template reference variable to our <ng-template> element and then conditionally ngIf will display it.

3. <ng-template> with ngSwitch

Find the example to use <ng-template> with ngSwitch.
import { Component } from '@angular/core';

   selector: 'app-ngswitch',
   templateUrl: './ng-template-ngswitch.component.html'   
export class NgTemplateNgSwitchComponent { 
   myDir= '';
<h3>ng-template with ngSwitch</h3>
<input type="radio" name="direction" (click)="myDir='east'">East
<input type="radio" name="direction" (click)="myDir='west'">West
<input type="radio" name="direction" (click)="myDir='north'">North
<input type="radio" name="direction" (click)="myDir='south'">South
<div [ngSwitch]="myDir">
  <ng-template [ngSwitchCase]= "'east'"> Go to <b>East</b> Direction </ng-template>
  <ng-template [ngSwitchCase]= "'west'"> Go to <b>West</b> Direction </ng-template>
  <ng-template [ngSwitchCase]= "'north'"> Go to <b>North</b> Direction </ng-template>
  <ng-template [ngSwitchCase]= "'south'">Go to <b>South</b> Direction </ng-template>
  <ng-template ngSwitchDefault> No Direction </ng-template>

4. <ng-template> with TemplateRef and ViewContainerRef

Here we will use <ng-template> with TemplateRef and ViewContainerRef. First we will assign a template reference variable to <ng-template> and then we will access its reference as TemplateRef using @ViewChild(). Now we will fetch the view container reference using a directive. We will fetch all view container references provided by directive using @ViewChildren() and QueryList. Then we will embed template reference to all view container references obtained by directive. Find the example.
import { Directive, ViewContainerRef } from '@angular/core';

   selector: '[cpMsg]' 
export class MessageDirective {
   constructor(public viewContainerRef: ViewContainerRef) { }
import { Component, ViewChild, ViewChildren, AfterViewInit, TemplateRef, ViewContainerRef, QueryList } from '@angular/core';
import { MessageDirective } from './message.directive';

   selector: 'app-msg',
   templateUrl: './cp-msg.component.html'
export class CpMsgComponent implements  AfterViewInit { 
    private msgTempRef = {} as TemplateRef<any>;
    private queryList = {} as QueryList<MessageDirective>; 
    ngAfterViewInit() { => 
<h3>ng-template with TemplateRef and ViewContainerRef</h3>

<ng-template #msg>
   Welcome to you.<br/>
   Happy learning!

<h3>Message </h3>

<div cpMsg> </div>

<h3>Message </h3>

<div cpMsg> </div> 
When we run application, the HTML code enclosed by <ng-template> will be embedded wherever we have used our cpMsg directive in our HTML template.

5. <ng-template> with Custom Structural Directive

Here we will provide example of <ng-template> with custom structural directive.
import { Directive, TemplateRef, ViewContainerRef, Input } from '@angular/core';

     selector: '[cpIf]' 
export class CpIfDirective {
	constructor( private templateRef: TemplateRef<any>,
	             private viewContainer: ViewContainerRef) { }
	@Input() set cpIf(condition: boolean) {
	   if (condition) {
	   } else {
import { Component } from '@angular/core';

   selector: 'app-cpif',
   templateUrl: './cp-if-demo.component.html'
export class CpIfDemoComponent { 
   showCpIf = '';
<h3>ng-template with Custom Structural Directive</h3>
  Show Message:
  <input type="radio" name="rad" (click)= "showCpIf= 'yes'"> Yse
  <input type="radio" name="rad" (click)= "showCpIf= 'no'"> No
<div *cpIf="showCpIf === 'yes'">
  Hello cpIf Directive.
<ng-template [cpIf]="showCpIf === 'no'">
      Message not Available.

Application Module and Component

import { Component } from '@angular/core';

   selector: 'app-root',
   template: `
export class AppComponent { 
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

import { AppComponent }  from './app.component';
import { NgTemplateNgForComponent } from './ng-template-ngfor.component';
import { NgTemplateNgIfComponent } from './ng-template-ngif.component';
import { NgTemplateNgSwitchComponent } from './ng-template-ngswitch.component';
import { CpIfDemoComponent } from './cp-if-demo.component';
import { CpMsgComponent } from './cp-msg.component';
import { MessageDirective } from './message.directive';
import { CpIfDirective } from './cp-if.directive';

  imports: [     
  declarations: [
  providers: [

  bootstrap: [
export class AppModule { } 

Run Demo Application

To run the demo application, find following steps.
1. Download source code using download link given below on this page.
2. Use downloaded src in your angular CLI application. To install angular CLI, find the link.
3. Run ng serve using command prompt.
4. Now access the URL http://localhost:4200
Angular <ng-template> Example


Structural Directives
Angular NgIf-Then-Else Example

Download Source Code


©2023 | Privacy Policy | Contact Us