Master Angular From Scratch

Learn Angular with clear, interactive, and structured lessons for beginners and developers.

ANGULAR COURSE SYLLABUS
1. Angular Introduction
Angular Introduction Angular History Angular Fundamentals Angular Architecture Component-Based Angular
2. Angular Setup
Angular Setup Introduction Installing Node.js Installing Angular CLI Creating an Angular Application Angular Project Structure
3. Components
Components Overview Component Anatomy Creating Components Component Selector Component Template Component Styles Component Metadata Standalone Components Parent and Child Components Reusable Components
4. Templates
Templates Introduction Template Syntax Template Expressions Template Statements Template Reference Variables ng-template ng-container ngTemplateOutlet Built-in Control Flow
5. Data Binding
Data Binding Introduction Interpolation Binding Property Binding Event Binding Two-Way Data Binding Class & Style Binding
6. Directives
Directives Introduction Attribute Directives Structural Directives Custom Directives
7. Pipes
Pipes Introduction Built-in Pipes Parameterized Pipes Chaining Pipes Custom Pipes
8. Event Handling
Event Handling Introduction DOM Events Event Binding Syntax Event Object Passing Data to Event Handlers
9. Input & Output
Component Communication @Input @Output EventEmitter
10. Lifecycle Hooks
Lifecycle Hooks Introduction ngOnInit ngOnChanges ngAfterViewInit ngOnDestroy
11. Services
Services Introduction Creating Angular Services Using Services in Components Sharing Data Through Services Service Scope
12. Dependency Injection
Dependency Injection Introduction Injecting Dependencies InjectionToken Provider Configuration
13. Routing
Routing Introduction Route Configuration RouterLink & Navigation Route Parameters Child Routes Route Guards
14. Forms
Angular Forms Introduction Template-Driven Forms Reactive Forms Form Validation Form Submission & Error Handling
15. HttpClient
HttpClient Introduction Configuring HttpClient GET Request POST, PUT & DELETE Requests Headers & Parameters HTTP Error Handling
16. Observables
Observables Introduction Creating & Subscribing to Observables Observable Data Streams Observable Operators Subscription Management
17. RxJS
RxJS Introduction RxJS Operators Transformation & Filtering Combining Observables Higher-Order Mapping Subjects
18. API Integration
API Integration Introduction Connecting Angular with REST API Consuming API Data CRUD API Integration API Response & Error Handling

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

1
Template Definition

Define reusable HTML inside ng-template.

→
2
Angular Stores

Angular keeps the template as a template fragment.

→
3
Condition / Context

Angular determines when the template is required.

→
4
Rendered UI

The template is created and displayed in the page.

Main Concepts

T
Template Definition

Defines a block of HTML that Angular can render later.

C
Conditional Rendering

A template can be displayed based on a condition.

R
Template Reference

A reference variable can identify a particular template.

D
Dynamic Rendering

Angular can create template content when it is required.

Basic ng-template

The following example defines a template containing a welcome message.

HTML Template
<ng-template>
    <p>Welcome Sam!</p>
</ng-template>
Important:
The paragraph is not displayed automatically. The ng-template only defines the template content.

Template Reference Variable

A template reference variable can be used to identify an ng-template.

Template Reference
<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.

Conditional Template
<div *ngIf="isLoggedIn; else loginTemplate">
    Welcome Sam
</div>

<ng-template #loginTemplate>
    <p>Please login to continue.</p>
</ng-template>
Example Output:

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.

Example
<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.

Template Context Example
<ng-template #studentTemplate let-name="studentName">
    <p>Student: {{ name }}</p>
</ng-template>
Concept:

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

1
Fallback Content

Display alternative content when a condition is false.

2
Conditional UI

Create template fragments for different UI states.

3
Reusable Templates

Define a template once and use it where Angular needs it.

4
Dynamic Content

Support scenarios where template content is created dynamically.

Complete ng-template Example

Student 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

1. Use meaningful template references

Choose descriptive names such as #loginTemplate or #noStudent.

2. Keep templates focused

Keep each template fragment focused on a specific UI purpose.

3. Use ng-template when deferred rendering is needed

Do not use ng-template simply as a replacement for normal HTML elements.

4. Keep template logic simple

Complex application logic should remain in the component class or services.

5. Understand related template APIs

Learn ng-container and ngTemplateOutlet when working with advanced template rendering.

Summary

  • ng-template defines 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.