0Pricing
Angular Academy · Lesson

AfterView and AfterContent Hooks

Run logic after view and content init.

AfterView and AfterContent Hooks is a free Angular Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Angular Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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 -> AfterViewChecked

The 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.

Frequently asked questions

Is the “AfterView and AfterContent Hooks” lesson free?

Yes — the full text of “AfterView and AfterContent Hooks” is free to read here on the web, and the Angular Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Angular Academy course, upgrade to CoddyKit PRO.

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; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

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

  1. ngOnInit and Initialization
  2. ngOnChanges and Input Changes
  3. ngOnDestroy and Cleanup
  4. AfterView and AfterContent Hooks
← Back to Angular Academy