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

Angular Lifecycle Hooks

Learn how Angular lifecycle hooks allow you to execute code at specific stages of a component's lifecycle.

What are Lifecycle Hooks?

Angular components go through different stages from creation to destruction. These stages are called the component lifecycle.

Angular provides lifecycle hook methods that allow developers to execute custom code when a particular lifecycle stage occurs.

Lifecycle hooks are useful for initialization, responding to changes, working with the view, and performing cleanup operations.

Angular Component Lifecycle

Component Created Creation
→
@OnChanges Input Changes
→
@OnInit Initialization
→
@AfterViewInit View Ready
→
@OnDestroy Cleanup

Common Lifecycle Hooks

1
@OnChanges

Runs when an Input property receives a new value.

2
@OnInit

Runs once after Angular initializes the component.

3
@AfterViewInit

Runs after Angular initializes the component view.

4
@OnDestroy

Runs just before Angular destroys the component.

@OnInit

The @OnInit lifecycle hook runs once after Angular has initialized the component's data-bound properties.

It is commonly used for initialization tasks such as loading course data, calling services, or preparing component properties.

Example
import {
    Component,
    OnInit
} from '@angular/core';

@Component({
    selector: 'app-courses',
    templateUrl: './courses.component.html'
})
export class CoursesComponent
    implements OnInit {

    courses: string[] = [];

    ngOnInit() {

        this.courses = [
            'Java Full Stack',
            'Python Full Stack',
            'Full Stack .NET'
        ];

    }

}

@OnChanges

The @OnChanges hook runs when one or more @Input properties change.

It is useful when a child component needs to respond to new data received from its parent.

Example
import {
    Component,
    Input,
    OnChanges,
    SimpleChanges
} from '@angular/core';

@Component({
    selector: 'app-course-details',
    templateUrl: './course-details.component.html'
})
export class CourseDetailsComponent
    implements OnChanges {

    @Input() courseName = '';

    ngOnChanges(changes: SimpleChanges) {

        console.log(
            'Course changed:',
            changes['courseName']
        );

    }

}

@AfterViewInit

The @AfterViewInit hook runs after Angular has completely initialized the component's view.

It can be useful when code needs to work with elements that are available after the view has been initialized.

Example
import {
    Component,
    AfterViewInit
} from '@angular/core';

@Component({
    selector: 'app-course',
    templateUrl: './course.component.html'
})
export class CourseComponent
    implements AfterViewInit {

    ngAfterViewInit() {

        console.log(
            'Course view initialized'
        );

    }

}

@OnDestroy

The @OnDestroy hook runs immediately before Angular destroys a component.

It is commonly used for cleanup tasks such as unsubscribing from Observables, removing event listeners, or stopping timers.

Example
import {
    Component,
    OnDestroy
} from '@angular/core';

@Component({
    selector: 'app-course',
    templateUrl: './course.component.html'
})
export class CourseComponent
    implements OnDestroy {

    ngOnDestroy() {

        console.log(
            'Course component destroyed'
        );

    }

}

Lifecycle Hooks Comparison

Hook When It Runs Common Use
@OnChanges When Input values change React to new Input data
@OnInit After component initialization Initialize data
@AfterViewInit After view initialization Work with initialized view
@OnDestroy Before component destruction Cleanup operations

Complete CIIT Training Institute Example

Suppose CIIT Training Institute has a course details component. When the selected course changes, the component receives the new value through @Input.

The component can use lifecycle hooks to initialize course information, respond to changes, work with its view, and perform cleanup.

Course Details Component
import {
    Component,
    Input,
    OnChanges,
    OnInit,
    AfterViewInit,
    OnDestroy,
    SimpleChanges
} from '@angular/core';

@Component({
    selector: 'app-course-details',
    templateUrl: './course-details.component.html'
})
export class CourseDetailsComponent
    implements
        OnChanges,
        OnInit,
        AfterViewInit,
        OnDestroy {

    @Input() courseName = '';

    ngOnChanges(changes: SimpleChanges) {

        console.log('Course changed');

    }

    ngOnInit() {

        console.log('Component initialized');

    }

    ngAfterViewInit() {

        console.log('View initialized');

    }

    ngOnDestroy() {

        console.log('Component destroyed');

    }

}

Lifecycle Execution Flow

@OnChanges Input Changes
→
@OnInit Initialize
→
@AfterViewInit View Ready
→
@OnDestroy Cleanup

Important Points

1. Initialization

Use @OnInit for component initialization tasks.

2. Input Changes

Use @OnChanges when an @Input property changes.

3. View Initialization

Use @AfterViewInit when the component view has been initialized.

4. Cleanup

Use @OnDestroy for cleanup operations before the component is removed.

Lifecycle Hooks Best Practices

  • Use the appropriate lifecycle hook for each task.
  • Keep initialization logic inside @OnInit.
  • Use @OnChanges when responding to Input changes.
  • Use @AfterViewInit only when the initialized view is required.
  • Perform cleanup operations inside @OnDestroy.
  • Keep lifecycle methods short and focused.

Summary

Angular lifecycle hooks allow developers to execute code at important stages of a component's lifecycle. @OnChanges handles Input changes, @OnInit handles initialization, @AfterViewInit handles view initialization, and @OnDestroy is used for cleanup before a component is destroyed.