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

Structural Directives

Learn how Angular structural directives control the creation, removal, and repetition of elements in the DOM.

What are Structural Directives?

Structural directives are Angular directives that change the structure of the DOM by adding, removing, or repeating elements.

They are commonly used for conditional rendering and displaying collections of data.

Structural Directive Flow

1
Condition

Angular evaluates a condition or collection.

→
2
Directive

The structural directive processes the expression.

→
3
DOM

Angular creates or removes template content.

→
4
UI

The required content appears on the page.

Common Structural Directives

IF
*ngIf

Displays an element when a condition is true.

FOR
*ngFor

Repeats an element for every item in a collection.

SW
*ngSwitch

Displays content based on a selected case.

C
Control Flow

Modern Angular also provides built-in control-flow syntax.

Using *ngIf

*ngIf conditionally adds or removes an element from the DOM.

Component
isLoggedIn = true;
HTML
<div *ngIf="isLoggedIn">
    Welcome to CIIT Training Institute
</div>
Result:
The welcome message is displayed when isLoggedIn is true.

Using *ngIf with else

An alternative template can be displayed when the condition is false.

HTML
<div *ngIf="isLoggedIn; else loginMessage">
    Welcome Student
</div>

<ng-template #loginMessage>
    Please Login
</ng-template>
Result:
Angular displays either the welcome message or the login message.

Using *ngFor

*ngFor repeats an element for every item in an array.

Component
students = [
    'Pradnya',
    'Rahul',
    'Amit'
];
HTML
<ul>

    <li *ngFor="let student of students">
        {{student}}
    </li>

</ul>
Result:
Angular creates one list item for each student.

Using Index with *ngFor

The index value can be used to display the position of each item.

HTML
<div *ngFor="let student of students; let i = index">

    {{i + 1}} - {{student}}

</div>
Output:
1 - Pradnya
2 - Rahul
3 - Amit

Using *ngSwitch

*ngSwitch can be used when different content needs to be displayed according to a value.

Component
course = 'Angular';
HTML
<div [ngSwitch]="course">

    <p *ngSwitchCase="'Angular'">
        Angular Course
    </p>

    <p *ngSwitchCase="'Java'">
        Java Course
    </p>

    <p *ngSwitchDefault>
        Course Not Found
    </p>

</div>
Result:
Angular Course

How Structural Directives Affect the DOM

+
Create

Angular creates template content when required.

−
Remove

Angular removes content when a condition is false.

R
Repeat

Angular repeats content for collection items.

S
Switch

Angular selects content according to a value.

Modern Angular Control Flow

Modern Angular also provides built-in control-flow syntax such as @if, @for, and @switch.

Example
@if (isLoggedIn) {
    <p>Welcome Student</p>
}

@for (student of students; track student) {
    <p>{{student}}</p>
}
Note:
Modern Angular applications can use the built-in control-flow syntax for conditional and repeated content.

Structural Directive Comparison

Directive Purpose Example
*ngIf Conditional rendering *ngIf="isLoggedIn"
*ngFor Repeat elements *ngFor="let student of students"
*ngSwitch Multiple conditions [ngSwitch]="course"
@if Modern conditional rendering @if (condition)
@for Modern repeated rendering @for (item of items)

Complete Student Example

student.component.ts
export class StudentComponent {

    isLoggedIn = true;

    students = [
        'Pradnya',
        'Rahul',
        'Amit'
    ];

}
student.component.html
<h3 *ngIf="isLoggedIn">
    Welcome to Angular Training
</h3>

<ul>

    <li *ngFor="let student of students">
        {{student}}
    </li>

</ul>

Welcome to Angular Training

Students:

Pradnya   |   Rahul   |   Amit

Best Practices

  • Use conditional rendering only when necessary.
  • Use tracking when rendering large collections.
  • Keep structural directive expressions simple.
  • Use modern Angular control flow in new Angular applications where appropriate.
  • Avoid unnecessary nested structural directives.
  • Keep template logic readable and maintainable.

Summary

  • Structural directives change the structure of the DOM.
  • *ngIf provides conditional rendering.
  • *ngFor repeats content for collection items.
  • *ngSwitch selects content based on a value.
  • Modern Angular provides @if, @for, and @switch.
  • Structural directives are useful for dynamic UI rendering.