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
Basic Syntax
To use this lifecycle hook, implement the AfterViewInit interface and define the ngAfterViewInit() method.
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.
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';
}
}
<h2>
{{ message }}
</h2>
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.
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
);
}
}
<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.
export class CourseCardComponent {
courseName = 'Java Full Stack';
showCourse(): void {
console.log(this.courseName);
}
}
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();
}
}
<app-course-card>
</app-course-card>
4. @ViewChild and View Initialization Flow
5. Working with View References
A component may use view references to access different elements that are required after view initialization.
@ViewChild('courseTitle')
courseTitle!: ElementRef;
@ViewChild('courseButton')
courseButton!: ElementRef;
ngAfterViewInit(): void {
console.log(
this.courseTitle.nativeElement.textContent
);
console.log(
this.courseButton.nativeElement.textContent
);
}
<h2 #courseTitle>
Python Full Stack
</h2>
<button #courseButton>
Enquire Now
</button>
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.
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
);
}
}
<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>
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.