ngAfterContentInit helps you know when the content you put inside a component from outside is ready. This is useful to work with that content safely.
ngAfterContentInit for projected content in Angular
import { AfterContentInit } from '@angular/core'; export class YourComponent implements AfterContentInit { ngAfterContentInit() { // code to run after projected content is initialized } }
ngAfterContentInit is a lifecycle hook method Angular calls once after the projected content is initialized.
To use it, your component class must implement the AfterContentInit interface.
ChildComponent is ready.import { AfterContentInit, Component } from '@angular/core'; @Component({ selector: 'child-comp', template: `<ng-content></ng-content>` }) export class ChildComponent implements AfterContentInit { ngAfterContentInit() { console.log('Projected content is ready'); } }
#projected and logs its text after content init.import { AfterContentInit, Component, ContentChild, ElementRef } from '@angular/core'; @Component({ selector: 'child-comp', template: `<ng-content></ng-content>` }) export class ChildComponent implements AfterContentInit { @ContentChild('projected') projectedElement!: ElementRef; ngAfterContentInit() { console.log('Projected element text:', this.projectedElement.nativeElement.textContent); } }
This example shows a parent component projecting a paragraph into ChildComponent. The child uses ngAfterContentInit to log when the content is ready and prints the paragraph text.
import { Component, AfterContentInit, ContentChild, ElementRef } from '@angular/core'; @Component({ selector: 'child-comp', template: `<div><ng-content></ng-content></div>` }) export class ChildComponent implements AfterContentInit { @ContentChild('info') infoElement!: ElementRef; ngAfterContentInit() { console.log('Content inside child component is ready.'); console.log('Projected content text:', this.infoElement.nativeElement.textContent.trim()); } } @Component({ selector: 'app-root', template: `<child-comp><p #info>Hello from projected content!</p></child-comp>` }) export class AppComponent {}
ngAfterContentInit runs only once after the first content projection is done.
If the projected content changes later, use ngAfterContentChecked to detect updates.
Always check if @ContentChild or @ContentChildren are defined before using them to avoid errors.
ngAfterContentInit tells you when projected content is ready inside your component.
Use it to safely access or manipulate content passed from outside.
It runs once after Angular inserts the external content into your component.
Practice
ngAfterContentInit lifecycle hook in Angular?Solution
Step 1: Understand the role of
This hook runs after Angular projects external content into the component'sngAfterContentInit<ng-content>slot.Step 2: Identify when to use it
It is used to safely access or manipulate the projected content once it is ready, not before.Final Answer:
To run code after projected content has been inserted into the component -> Option AQuick Check:
ngAfterContentInit = runs after projected content ready [OK]
- Confusing ngAfterContentInit with ngOnInit
- Trying to access projected content before this hook
- Using it to detect input property changes
ngAfterContentInit in an Angular component?Solution
Step 1: Recall Angular lifecycle hook syntax
Lifecycle hooks are implemented as methods on the component class without assignment or arrow functions.Step 2: Check each option
ngAfterContentInit() { console.log('Content initialized'); } uses the correct method syntax. Arrow functions or assignments are not valid lifecycle hook implementations. A standalone function is not a class method.Final Answer:
ngAfterContentInit() { console.log('Content initialized'); } -> Option AQuick Check:
Lifecycle hooks are class methods, not arrow functions [OK]
- Using arrow functions for lifecycle hooks
- Defining lifecycle hooks outside the class
- Assigning lifecycle hooks to variables
import { Component, AfterContentInit, ContentChild, ElementRef } from '@angular/core';
@Component({
selector: 'child-comp',
template: ``
})
export class ChildComp implements AfterContentInit {
@ContentChild('projected') projectedContent!: ElementRef;
ngAfterContentInit() {
console.log(this.projectedContent.nativeElement.textContent.trim());
}
}
// Usage in parent template:
// Hello World
Solution
Step 1: Understand ContentChild and ngAfterContentInit
The@ContentChild('projected')decorator gets the element with template reference#projectedprojected inside<ng-content>. This is available after content initialization.Step 2: Check what ngAfterContentInit logs
It logs the trimmed text content of the projected element, which is 'Hello World'.Final Answer:
Hello World -> Option CQuick Check:
ContentChild element text = 'Hello World' [OK]
- Trying to access ContentChild before ngAfterContentInit
- Forgetting to use template reference variable
- Assuming projected content is undefined
import { Component, AfterContentInit, ContentChild, ElementRef } from '@angular/core';
@Component({
selector: 'proj-comp',
template: ``
})
export class ProjComp implements AfterContentInit {
@ContentChild('contentRef') content!: ElementRef;
ngAfterContentInit() {
console.log(this.content.nativeElement.textContent);
}
}When used as
<proj-comp><p>Test</p></proj-comp>, what error will occur and why?Solution
Step 1: Analyze ContentChild selector
The@ContentChild('contentRef')looks for an element with template reference variable#contentRefinside projected content.Step 2: Check usage in parent template
The projected<p>Test</p>has no#contentRefvariable, socontentis undefined.Step 3: Understand consequence in ngAfterContentInit
Accessingthis.content.nativeElementcauses a runtime error becausecontentis undefined.Final Answer:
Error: content is undefined because no template reference variable 'contentRef' is present -> Option BQuick Check:
Missing template ref causes undefined ContentChild [OK]
- Assuming ContentChild finds elements without template refs
- Ignoring runtime errors from undefined properties
- Confusing ngAfterContentInit with ngOnInit timing
ngAfterContentInit to achieve this?import { Component, AfterContentInit, ContentChildren, QueryList, ElementRef } from '@angular/core';
@Component({
selector: 'highlight-list',
template: ``
})
export class HighlightList implements AfterContentInit {
@ContentChildren('item') items!: QueryList<ElementRef>;
ngAfterContentInit() {
// What should go here?
}
}Options for
ngAfterContentInit implementation:Solution
Step 1: Understand ContentChildren and QueryList
@ContentChildren('item')collects all projected elements with template ref#iteminto a QueryList. Access the first element withitems.first.Step 2: Apply style to first projected item
Useitems.first.nativeElement.style.backgroundColorto highlight the first item safely after content is initialized.Step 3: Evaluate other options
this.items.nativeElement.style.backgroundColor = 'yellow'; is invalid because QueryList has no nativeElement property. this.items[0].nativeElement.style.backgroundColor = 'yellow'; is invalid because QueryList is not an array. document.querySelector('item').style.backgroundColor = 'yellow'; uses direct DOM query which breaks Angular encapsulation.Final Answer:
this.items.first.nativeElement.style.backgroundColor = 'yellow'; -> Option DQuick Check:
Use QueryList.first to access first projected element [OK]
- Treating QueryList as an array
- Using direct DOM queries instead of Angular references
- Accessing nativeElement on QueryList itself
Ready to prove it?
Pass the test and this pattern moves unknown
