0
0
Angularframework~5 mins

ngAfterContentInit for projected content in Angular - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is ngAfterContentInit in Angular?

ngAfterContentInit is a lifecycle hook that runs once after Angular projects external content into the component's view. It lets you act when the projected content is ready.

Click to reveal answer
beginner
When exactly does ngAfterContentInit run in relation to content projection?

It runs after Angular inserts the external content inside <ng-content> tags but before the view is fully rendered.

Click to reveal answer
intermediate
How do you access projected content inside ngAfterContentInit?

You use @ContentChild or @ContentChildren decorators to get references to the projected elements, then use them inside ngAfterContentInit.

Click to reveal answer
intermediate
Why can't you access projected content in the constructor or ngOnInit?

Because the projected content is not yet inserted into the component at those stages. ngAfterContentInit waits until projection is done.

Click to reveal answer
intermediate
Give a simple example of using ngAfterContentInit to log projected content.
<pre>import { Component, AfterContentInit, ContentChild, ElementRef } from '@angular/core';

export class MyComponent implements AfterContentInit {
  @ContentChild('projected') projectedContent!: ElementRef;

  ngAfterContentInit() {
    console.log('Projected content:', this.projectedContent.nativeElement.textContent);
  }
}</pre>
Click to reveal answer
When does ngAfterContentInit run in Angular?
AAfter projected content is inserted
BBefore constructor runs
CBefore <code>ngOnInit</code>
DAfter view is fully rendered
Which decorator is used to access projected content inside ngAfterContentInit?
A@ViewChild
B@ContentChild
C@Input
D@Output
Why can't you access projected content in ngOnInit?
ABecause <code>ngOnInit</code> is deprecated
BAngular forbids it
CIt causes errors
DContent is not projected yet
What Angular tag is used to mark where content is projected?
A<ng-template>
B<ng-container>
C<ng-content>
D<ng-project>
Which lifecycle hook runs after ngAfterContentInit?
AngAfterContentChecked
BngOnDestroy
CngOnChanges
DngAfterViewInit
Explain the purpose of ngAfterContentInit and how it relates to projected content in Angular.
Think about when Angular inserts external content into your component.
You got /4 concepts.
    Describe how you would use ngAfterContentInit to interact with content passed from a parent component.
    Focus on accessing and using the projected content.
    You got /4 concepts.