AfterView and AfterContent Hooks
Run logic after view and content init.
AfterView and AfterContent Hooks is a free Angular Academy lesson on CoddyKit. This is lesson 4 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Angular Academy learning path, and your progress syncs across the web and the CoddyKit app. The Angular Academy course includes 4 lessons in total.
View vs Content
Angular distinguishes a component view (its own template) from content projected into it via ng-content. Separate hooks fire for each.
AfterViewInit
The AfterViewInit hook runs once after the component view and its child views are fully initialized. It is the right time to access view children.
import { AfterViewInit } from "@angular/core";
export class DemoComponent implements AfterViewInit {
ngAfterViewInit(): void {
console.log("view ready");
}
}Accessing ViewChild
A @ViewChild reference to a template element is reliably available in ngAfterViewInit, not earlier.
@ViewChild("input") input!: ElementRef;
ngAfterViewInit() {
this.input.nativeElement.focus();
}AfterViewChecked
The AfterViewChecked hook runs after every check of the component view. It fires often, so keep its work minimal.
ngAfterViewChecked() {
// runs after each view check
}AfterContentInit
The AfterContentInit hook runs once after content projected with ng-content has been initialized.
import { AfterContentInit } from "@angular/core";
export class CardComponent implements AfterContentInit {
ngAfterContentInit() {
console.log("projected content ready");
}
}Accessing ContentChild
Use @ContentChild to query projected content. The reference is available in ngAfterContentInit.
@ContentChild(HeaderComponent) header!: HeaderComponent;
ngAfterContentInit() {
console.log(this.header);
}AfterContentChecked
The AfterContentChecked hook runs after every check of the projected content. Like the view-checked hook, it fires frequently.
Full Hook Order
For a component with inputs and content, the order is: ngOnChanges, ngOnInit, ngDoCheck, ngAfterContentInit, ngAfterContentChecked, ngAfterViewInit, ngAfterViewChecked.
// init sequence:
// OnChanges -> OnInit -> DoCheck
// -> AfterContentInit -> AfterContentChecked
// -> AfterViewInit -> AfterViewCheckedThe ExpressionChangedAfterChecked Error
Changing bound data inside ngAfterViewInit can trigger the famous ExpressionChangedAfterItHasBeenCheckedError. Defer such updates to avoid it.
ngAfterViewInit() {
// defer to next tick to avoid the error
setTimeout(() => this.ready = true);
}Using ViewChildren
@ViewChildren returns a QueryList of multiple matching elements or components, ready in ngAfterViewInit.
@ViewChildren(ItemComponent) items!: QueryList<ItemComponent>;
ngAfterViewInit() {
console.log(this.items.length);
}Choosing the Right Hook
Use the AfterView hooks for your own template and the AfterContent hooks for projected content. Prefer the Init variants for one-time setup and the Checked variants only when necessary.
Quick Check
Test your understanding of the after-view and after-content hooks.
Recap
The AfterContent hooks fire for projected content and the AfterView hooks for the component own view. Init variants run once; Checked variants run on every cycle. Use them to access @ViewChild and @ContentChild safely.
Learn HTML with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 38
- Lessons
- 144
Frequently Asked Questions
Is the “AfterView and AfterContent Hooks” lesson free?
Yes — any 3 lessons of the Angular Academy learning path, including “AfterView and AfterContent Hooks”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Angular Academy course includes 4 lessons in total.
What will I learn in “AfterView and AfterContent Hooks”?
Run logic after view and content init. You practise Angular Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Angular Academy?
No prior experience is required. Angular Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.
How long does the “AfterView and AfterContent Hooks” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Angular Academy lesson?
Yes. Every Angular Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- ngOnInit and Initialization
- ngOnChanges and Input Changes
- ngOnDestroy and Cleanup
- AfterView and AfterContent Hooks