ngTemplateOutlet
Learn how Angular uses ngTemplateOutlet to render a
template dynamically and pass data into that template.
What is ngTemplateOutlet?
ngTemplateOutlet is an Angular directive used to
display an existing ng-template dynamically.
It is useful when the same template needs to be rendered in different places or when different data needs to be supplied to the same template.
ngTemplateOutlet Flow
Create Template
Define the reusable content inside ng-template.
Reference Template
Give the template a reference variable.
Outlet
Use ngTemplateOutlet to render the template.
Display
Angular creates the template content in the page.
Main Concepts
Template
Defines the HTML structure that can be rendered later.
Reference
Identifies the template using a reference variable.
Outlet
Renders the referenced template dynamically.
Context
Allows data to be passed into the template.
Basic ngTemplateOutlet Example
First, create a template and give it a reference.
<ng-template #welcomeTemplate>
<p>Welcome to CIIT Training Institute!</p>
</ng-template>
Now render the template using ngTemplateOutlet.
<ng-container
*ngTemplateOutlet="welcomeTemplate">
</ng-container>
Welcome to CIIT Training Institute!
Why Use ng-container with ngTemplateOutlet?
ng-container provides a logical location where the
template can be rendered without adding another HTML wrapper.
<ng-container
*ngTemplateOutlet="studentTemplate">
</ng-container>
Angular renders the content of
studentTemplate at the location of the
ng-container.
Reusable Template
The same template can be rendered more than once.
<ng-template #messageTemplate>
<p>Welcome to Angular!</p>
</ng-template>
<ng-container
*ngTemplateOutlet="messageTemplate">
</ng-container>
<ng-container
*ngTemplateOutlet="messageTemplate">
</ng-container>
Welcome to Angular!
Welcome to Angular!
ngTemplateOutlet with Context
A context object can be passed to the template using
ngTemplateOutletContext.
<ng-template
#studentTemplate
let-name="studentName">
<p>
Student: {{ name }}
</p>
</ng-template>
<ng-container
*ngTemplateOutlet="
studentTemplate;
context: { studentName: 'Sam' }
">
</ng-container>
Student: Sam
Passing Multiple Context Values
More than one value can be passed to a template.
<ng-template
#studentTemplate
let-name="name"
let-course="course">
<p>
Name: {{ name }}
</p>
<p>
Course: {{ course }}
</p>
</ng-template>
<ng-container
*ngTemplateOutlet="
studentTemplate;
context: {
name: 'Sam',
course: 'Angular Full Stack'
}
">
</ng-container>
Template Rendering Flow
Template
#studentTemplate
Outlet
ngTemplateOutlet
Context
studentName
UI
Student: Sam
ng-template vs ngTemplateOutlet
| Feature | ng-template | ngTemplateOutlet |
|---|---|---|
| Purpose | Defines a template | Renders a template |
| Creates UI directly | No | Yes |
| Reference variable | Commonly used | Uses a template reference |
| Context support | Defines local variables | Can pass context |
| Dynamic rendering | Defines the content | Performs the rendering |
Common Uses of ngTemplateOutlet
Reusable UI
Render the same template in multiple locations.
Dynamic Templates
Select and render templates dynamically.
Template Context
Pass different data to the same template.
Clean DOM
Combine template rendering with ng-container.
Complete ngTemplateOutlet Example
<ng-template
#studentCard
let-name="name"
let-course="course">
<div class="student-card">
<h3>{{ name }}</h3>
<p>
Course: {{ course }}
</p>
</div>
</ng-template>
<ng-container
*ngTemplateOutlet="
studentCard;
context: {
name: 'Sam',
course: 'Angular Full Stack'
}
">
</ng-container>
Student Card
Student: Sam
Course: Angular Full Stack
Best Practices
Each template should have a clear and specific purpose.
Names such as #studentCard and
#loginTemplate make templates easier to understand.
Pass data through ngTemplateOutletContext when
the same template needs different values.
This keeps the rendered DOM cleaner.
Keep business logic inside components or services instead of placing complex logic inside templates.
Summary
ng-templatedefines a template fragment.ngTemplateOutletrenders an existing template.- A template reference identifies the template to render.
ngTemplateOutletContextcan pass data to a template.- The same template can be rendered multiple times.
ng-containercan provide a clean rendering location.