ng-template
Learn how Angular uses ng-template to define template fragments
that are rendered only when Angular explicitly creates them.
What is ng-template?
ng-template is an Angular element used to define a
template fragment. The content inside an ng-template
is not displayed immediately when the page loads.
Angular creates and renders the template only when it is required, such as during conditional rendering or dynamic template operations.
ng-template Flow
Template Definition
Define reusable HTML inside ng-template.
Angular Stores
Angular keeps the template as a template fragment.
Condition / Context
Angular determines when the template is required.
Rendered UI
The template is created and displayed in the page.
Main Concepts
Template Definition
Defines a block of HTML that Angular can render later.
Conditional Rendering
A template can be displayed based on a condition.
Template Reference
A reference variable can identify a particular template.
Dynamic Rendering
Angular can create template content when it is required.
Basic ng-template
The following example defines a template containing a welcome message.
<ng-template>
<p>Welcome Sam!</p>
</ng-template>
ng-template only defines the template content.
Template Reference Variable
A template reference variable can be used to identify an
ng-template.
<ng-template #welcomeTemplate>
<p>Welcome to CIIT Training Institute!</p>
</ng-template>
Here #welcomeTemplate creates a reference to the
template. Angular can use this reference when the template needs
to be rendered.
ng-template with Conditional Rendering
One of the common uses of ng-template is providing an
alternative template for conditional rendering.
<div *ngIf="isLoggedIn; else loginTemplate">
Welcome Sam
</div>
<ng-template #loginTemplate>
<p>Please login to continue.</p>
</ng-template>
If isLoggedIn = true
Welcome Sam
If isLoggedIn = false
Please login to continue.
Using an Else Template
The else syntax allows Angular to render an
ng-template when the main condition is false.
Condition
isLoggedIn
True
Welcome Sam
False
loginTemplate
Template with Reference
The reference variable identifies the template that Angular should use when required.
<ng-template #studentMessage>
<p>Student is not available.</p>
</ng-template>
The name studentMessage becomes a reference to this
template fragment.
ng-template with Template Context
A template can also be designed to receive contextual information. This allows the same template structure to display different values.
<ng-template #studentTemplate let-name="studentName">
<p>Student: {{ name }}</p>
</ng-template>
The template receives a contextual value and displays it
through the local variable name.
ng-template vs Normal HTML Element
| Feature | ng-template | Normal HTML Element |
|---|---|---|
| Rendered immediately | No | Yes |
| Template fragment | Yes | No |
| Reference variable | Supported | Supported |
| Conditional rendering | Commonly used | Can contain conditional content |
| Dynamic template usage | Yes | Not directly |
Common Uses of ng-template
Fallback Content
Display alternative content when a condition is false.
Conditional UI
Create template fragments for different UI states.
Reusable Templates
Define a template once and use it where Angular needs it.
Dynamic Content
Support scenarios where template content is created dynamically.
Complete ng-template Example
<h3>Student Status</h3>
<div *ngIf="isStudentAvailable; else noStudent">
<p>Student: Sam</p>
<p>Course: Angular Full Stack</p>
</div>
<ng-template #noStudent>
<p>No student record found.</p>
</ng-template>
Student Status
Student: Sam
Course: Angular Full Stack
If the student is unavailable, Angular can render the
noStudent template instead.
Best Practices
Choose descriptive names such as
#loginTemplate or
#noStudent.
Keep each template fragment focused on a specific UI purpose.
Do not use ng-template simply as a replacement
for normal HTML elements.
Complex application logic should remain in the component class or services.
Learn ng-container and
ngTemplateOutlet when working with advanced
template rendering.
Summary
ng-templatedefines a template fragment.- Its content is not rendered immediately.
- Angular creates the template when it is required.
- Template reference variables identify template fragments.
- It is commonly used with conditional rendering.
- It can support contextual and dynamic template scenarios.