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

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

1
Create Template

Define the reusable content inside ng-template.

→
2
Reference Template

Give the template a reference variable.

→
3
Outlet

Use ngTemplateOutlet to render the template.

→
4
Display

Angular creates the template content in the page.

Main Concepts

T
Template

Defines the HTML structure that can be rendered later.

R
Reference

Identifies the template using a reference variable.

O
Outlet

Renders the referenced template dynamically.

C
Context

Allows data to be passed into the template.

Basic ngTemplateOutlet Example

First, create a template and give it a reference.

Template Definition
<ng-template #welcomeTemplate>
    <p>Welcome to CIIT Training Institute!</p>
</ng-template>

Now render the template using ngTemplateOutlet.

Template Outlet
<ng-container
    *ngTemplateOutlet="welcomeTemplate">
</ng-container>
Output:

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.

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

Reusable Template
<ng-template #messageTemplate>
    <p>Welcome to Angular!</p>
</ng-template>

<ng-container
    *ngTemplateOutlet="messageTemplate">
</ng-container>

<ng-container
    *ngTemplateOutlet="messageTemplate">
</ng-container>
Output:

Welcome to Angular!

Welcome to Angular!

ngTemplateOutlet with Context

A context object can be passed to the template using ngTemplateOutletContext.

Template with Context
<ng-template
    #studentTemplate
    let-name="studentName">

    <p>
        Student: {{ name }}
    </p>

</ng-template>
Passing Context
<ng-container
    *ngTemplateOutlet="
        studentTemplate;
        context: { studentName: 'Sam' }
    ">
</ng-container>
Output:

Student: Sam

Passing Multiple Context Values

More than one value can be passed to a template.

Template
<ng-template
    #studentTemplate
    let-name="name"
    let-course="course">

    <p>
        Name: {{ name }}
    </p>

    <p>
        Course: {{ course }}
    </p>

</ng-template>
Context
<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

1
Reusable UI

Render the same template in multiple locations.

2
Dynamic Templates

Select and render templates dynamically.

3
Template Context

Pass different data to the same template.

4
Clean DOM

Combine template rendering with ng-container.

Complete ngTemplateOutlet Example

Student Card Template
<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

1. Keep reusable templates focused

Each template should have a clear and specific purpose.

2. Use meaningful template references

Names such as #studentCard and #loginTemplate make templates easier to understand.

3. Use context for dynamic data

Pass data through ngTemplateOutletContext when the same template needs different values.

4. Use ng-container when a wrapper is unnecessary

This keeps the rendered DOM cleaner.

5. Avoid unnecessary template complexity

Keep business logic inside components or services instead of placing complex logic inside templates.

Summary

  • ng-template defines a template fragment.
  • ngTemplateOutlet renders an existing template.
  • A template reference identifies the template to render.
  • ngTemplateOutletContext can pass data to a template.
  • The same template can be rendered multiple times.
  • ng-container can provide a clean rendering location.