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 AfterViewInit

Angular @AfterViewInit is a lifecycle hook that runs after Angular has completely initialized the component's view and its child views.

What is @AfterViewInit?

The ngAfterViewInit() lifecycle hook is called after Angular finishes initializing the component view.

It is useful when code needs to work with elements or child components after the view has been created.

Simple Definition

ngAfterViewInit() is used when you need to perform an operation after the component's view and child views are initialized.

Where @AfterViewInit Fits in Angular Lifecycle

Component Created Angular creates component
→
View Initialized Component template is initialized
→
ngAfterViewInit() View initialization logic runs
→
View Ready Child views are available

Basic Syntax

To use this lifecycle hook, implement the AfterViewInit interface and define the ngAfterViewInit() method.

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

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

    ngAfterViewInit(): void {

        console.log('View initialized');

    }

}
Important

Angular automatically calls ngAfterViewInit() after the component view has been initialized.

Why Use @AfterViewInit?

View Access

Perform operations after the component view is initialized.

ngAfterViewInit()
Child Components

Work with initialized child component views.

ViewChild
Element References

Access elements referenced from the component view.

ElementRef
UI Initialization

Perform view-related initialization after rendering starts.

initializeView()

1. Simple AfterViewInit Example

The following example displays a message after the component view has been initialized.

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

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

    message = '';

    ngAfterViewInit(): void {

        this.message = 'CIIT Training Institute View Ready';

    }

}
Template
<h2>
    {{ message }}
</h2>
Expected Output:

CIIT Training Institute View Ready

2. Using @ViewChild with @AfterViewInit

@ViewChild can be used to obtain a reference to an element or child component after the view has been initialized.

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

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

    @ViewChild('courseTitle')
    courseTitle!: ElementRef;

    ngAfterViewInit(): void {

        console.log(
            this.courseTitle.nativeElement.textContent
        );

    }

}
Template
<h2 #courseTitle>
    Angular Full Stack
</h2>
How it works

The template reference #courseTitle is connected to @ViewChild. After the view is initialized, the element reference can be accessed inside ngAfterViewInit().

3. Accessing a Child Component

A parent component can use @ViewChild to access a child component after its view has been initialized.

Child Component
export class CourseCardComponent {

    courseName = 'Java Full Stack';

    showCourse(): void {

        console.log(this.courseName);

    }

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

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

    @ViewChild(CourseCardComponent)
    courseCard!: CourseCardComponent;

    ngAfterViewInit(): void {

        this.courseCard.showCourse();

    }

}
Parent Template
<app-course-card>
</app-course-card>
Expected Result:
Parent component can access the initialized child component.

4. @ViewChild and View Initialization Flow

Template Element / Child Component
→
@ViewChild Creates Reference
→
ngAfterViewInit() View becomes initialized
→
Access View Use the reference

5. Working with View References

A component may use view references to access different elements that are required after view initialization.

Component
@ViewChild('courseTitle')
courseTitle!: ElementRef;

@ViewChild('courseButton')
courseButton!: ElementRef;

ngAfterViewInit(): void {

    console.log(
        this.courseTitle.nativeElement.textContent
    );

    console.log(
        this.courseButton.nativeElement.textContent
    );

}
Template
<h2 #courseTitle>
    Python Full Stack
</h2>

<button #courseButton>
    Enquire Now
</button>
Expected View:

Python Full Stack

@OnInit vs @AfterViewInit

ngOnInit() ngAfterViewInit()
Used for component initialization logic. Used for view initialization logic.
Runs before the component view is fully initialized. Runs after the component view is initialized.
Commonly used for preparing data. Useful when working with view references.
Suitable for initial service or API operations. Suitable for view and child-component access.

Complete CIIT Training Institute Example

Suppose CIIT Training Institute has a course enquiry component. After the course view is initialized, the component can access a course heading and enquiry button using @ViewChild.

course-enquiry.component.ts
import {
    Component,
    AfterViewInit,
    ElementRef,
    ViewChild
} from '@angular/core';

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

    @ViewChild('courseHeading')
    courseHeading!: ElementRef;

    @ViewChild('enquiryButton')
    enquiryButton!: ElementRef;

    ngAfterViewInit(): void {

        console.log(
            'Course:',
            this.courseHeading.nativeElement.textContent
        );

        console.log(
            'Button:',
            this.enquiryButton.nativeElement.textContent
        );

    }

}
course-enquiry.component.html
<div>

    <h2 #courseHeading>
        Full Stack .NET
    </h2>

    <p>
        Learn Full Stack .NET at CIIT Training Institute.
    </p>

    <button
        #enquiryButton
        class="btn btn-primary">
        Enquire Now
    </button>

</div>
Expected Output:

Full Stack .NET

Learn Full Stack .NET at CIIT Training Institute.

Important Points About @AfterViewInit

  • ngAfterViewInit() runs after the component view is initialized.
  • It is useful when code depends on the initialized view.
  • @ViewChild references can be accessed after view initialization.
  • It can be used for accessing child component instances.
  • Avoid unnecessary DOM manipulation.
  • Keep view-related initialization logic simple and organized.

@AfterViewInit Best Practices

  • Use ngAfterViewInit only when logic depends on the initialized view.
  • Use @ViewChild carefully for view references.
  • Prefer Angular bindings and component logic over unnecessary direct DOM manipulation.
  • Keep view initialization code readable.
  • Avoid performing expensive operations unnecessarily.
  • Keep reusable application logic inside services or components.

Summary

Angular @AfterViewInit provides the ngAfterViewInit() lifecycle method for performing operations after the component view and child views have been initialized.

It is especially useful when working with @ViewChild, element references, and initialized child components.