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

Control Flow

Learn how Angular uses control flow to conditionally display, repeat, and switch between different template sections.

What is Control Flow?

Angular control flow determines which parts of a template should be displayed based on conditions, collections, or values.

Modern Angular provides built-in control flow blocks such as @if, @for, and @switch for creating dynamic user interfaces.

Control Flow

1
Data

Component provides the data used by the template.

→
2
Condition

Angular evaluates a condition or collection.

→
3
Control Block

Angular selects the required template content.

→
4
Rendered UI

The selected content appears on the page.

Main Control Flow Concepts

IF
@if

Displays content when a condition is true.

EL
@else

Displays alternative content when the condition is false.

FR
@for

Repeats template content for each item in a collection.

SW
@switch

Selects content based on different possible values.

@if Control Flow

The @if block displays template content only when the specified condition evaluates to true.

Basic @if Example
@if (isLoggedIn) {
    <p>Welcome Sam!</p>
}
Output:

Welcome Sam!

@if with @else

Use @else when you want to display alternative content when the condition is false.

@if and @else Example
@if (isLoggedIn) {
    <p>Welcome Sam!</p>
} @else {
    <p>Please login to continue.</p>
}
Condition True
Welcome Sam!
→
Condition False
Please login

@else if

Multiple conditions can be checked using @else if.

@else if Example
@if (marks >= 75) {
    <p>Grade: A</p>
} @else if (marks >= 60) {
    <p>Grade: B</p>
} @else {
    <p>Grade: C</p>
}
Example Output:

Grade: A

@for Control Flow

The @for block repeats template content for every item in a collection.

Basic @for Example
@for (student of students; track student.id) {
    <p>
        {{ student.name }}
    </p>
}
Example Output:

Sam

Pradnya

Rahul

Tracking Items with @for

The track expression helps Angular identify each item when the collection changes.

Track Example
@for (student of students; track student.id) {
    <div>
        <strong>
            {{ student.name }}
        </strong>
    </div>
}

Using a stable identifier such as student.id helps Angular efficiently update the rendered list.

@empty Block

The @empty block provides alternative content when the collection used by @for contains no items.

@for with @empty
@for (student of students; track student.id) {
    <p>
        {{ student.name }}
    </p>
} @empty {
    <p>No students found.</p>
}
When the collection is empty:

No students found.

@switch Control Flow

The @switch block is useful when different UI content should be displayed for different values.

@switch Example
@switch (course) {

    @case ('Angular') {
        <p>Angular Full Stack</p>
    }

    @case ('Java') {
        <p>Java Full Stack</p>
    }

    @case ('Dot Net') {
        <p>.NET Full Stack</p>
    }

    @default {
        <p>Course not available.</p>
    }
}
Example Output:

Angular Full Stack

Switch Flow

Value
course
→
Case
Angular
→
Template
Angular Full Stack

Traditional vs Modern Control Flow

Purpose Traditional Angular Modern Angular
Conditional rendering *ngIf @if
Alternative content ng-template @else
Looping *ngFor @for
Empty collection Separate condition @empty
Multiple cases ngSwitch @switch

Common Uses of Control Flow

1
Conditional UI

Show or hide content based on application state.

2
Lists

Display multiple records from a collection.

3
Empty States

Display useful content when no records are available.

4
Multiple Cases

Display different UI based on a selected value.

Complete Control Flow Example

Student Dashboard
@if (students.length > 0) {

    <h3>Students</h3>

    @for (student of students; track student.id) {

        <div>

            <h4>
                {{ student.name }}
            </h4>

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

        </div>
    }

} @else {

    <p>No students found.</p>

}
Student Dashboard

Student: Sam

Course: Angular Full Stack


Student: Pradnya

Course: Java Full Stack

Best Practices

1. Keep conditions simple

Avoid putting complex business logic directly inside control flow expressions.

2. Use modern control flow for new Angular code

Use @if, @for, and @switch when building modern Angular applications.

3. Use track with @for

Use a stable unique identifier such as student.id when rendering collections.

4. Handle empty collections

Use @empty to provide meaningful feedback when there are no records.

5. Keep template logic readable

Avoid deeply nested control flow and move complex logic into components or services.

Summary

  • @if is used for conditional rendering.
  • @else provides alternative content.
  • @for repeats content for collection items.
  • @empty handles an empty collection.
  • @switch handles multiple possible cases.
  • The track expression helps Angular identify list items efficiently.
  • Control flow makes Angular templates dynamic and data-driven.