0
0
Angularframework~20 mins

ngAfterContentInit for projected content in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Content Projection Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
When is ngAfterContentInit called in Angular?
Consider a component that uses content projection with <ng-content>. When does Angular call the ngAfterContentInit lifecycle hook?
ABefore the projected content is initialized but after the component's view is ready.
BImmediately after the component's constructor runs.
CAfter the component's own view and all projected content have been initialized.
DOnly after the component's view is fully initialized, ignoring projected content.
Attempts:
2 left
💡 Hint
Think about when Angular finishes setting up both the component's template and the content passed inside it.
state_output
intermediate
2:00remaining
What will be logged by ngAfterContentInit?
Given this Angular component and usage, what will the console log inside ngAfterContentInit?
Angular
import { Component, ContentChild, AfterContentInit, ElementRef } from '@angular/core';

@Component({
  selector: 'child-comp',
  template: `<ng-content></ng-content>`
})
export class ChildComp implements AfterContentInit {
  @ContentChild('projected') projectedContent!: ElementRef;

  ngAfterContentInit() {
    console.log(this.projectedContent.nativeElement.textContent.trim());
  }
}

// Usage in parent template:
// <child-comp><p #projected>Hello Angular</p></child-comp>
A"<p #projected>Hello Angular</p>"
B"Hello Angular"
CError: Cannot read property 'nativeElement' of undefined
Dundefined
Attempts:
2 left
💡 Hint
The @ContentChild decorator finds the element inside projected content after content init.
📝 Syntax
advanced
2:00remaining
Identify the correct ngAfterContentInit implementation
Which of the following ngAfterContentInit method implementations correctly logs the number of projected items using @ContentChildren?
Angular
import { Component, ContentChildren, QueryList, AfterContentInit, ElementRef } from '@angular/core';

@Component({
  selector: 'list-comp',
  template: `<ng-content></ng-content>`
})
export class ListComp implements AfterContentInit {
  @ContentChildren('item') items!: QueryList<ElementRef>;

  ngAfterContentInit() {
    // ???
  }
}
Aconsole.log(this.items.length);
Bconsole.log(this.items.count);
Cconsole.log(this.items.toArray().length);
Dconsole.log(this.items.size);
Attempts:
2 left
💡 Hint
QueryList has a length property directly to get the number of items.
🔧 Debug
advanced
2:00remaining
Why does @ContentChild return undefined in ngAfterContentInit?
A developer uses @ContentChild('proj') to get a projected element but finds it undefined inside ngAfterContentInit. What is the most likely cause?
Angular
import { Component, ContentChild, AfterContentInit, ElementRef } from '@angular/core';

@Component({
  selector: 'proj-comp',
  template: `<ng-content></ng-content>`
})
export class ProjComp implements AfterContentInit {
  @ContentChild('proj') projElement!: ElementRef;

  ngAfterContentInit() {
    console.log(this.projElement);
  }
}

// Usage:
// <proj-comp><div>Content without #proj</div></proj-comp>
AThe projected element does not have the template reference variable #proj.
BngAfterContentInit runs before content projection is done.
CThe component should use @ViewChild instead of @ContentChild.
DThe ElementRef type is incorrect for projected content.
Attempts:
2 left
💡 Hint
Check if the projected element has the exact template reference variable used in @ContentChild.
🧠 Conceptual
expert
3:00remaining
How does Angular handle multiple ngAfterContentInit hooks in nested components with projection?
Consider a parent component projecting content into a child component, which itself projects content into a grandchild component. Which statement best describes the order and behavior of ngAfterContentInit hooks in this nested projection scenario?
AAngular calls <code>ngAfterContentInit</code> only on the top-level component that contains the projection.
BAngular calls <code>ngAfterContentInit</code> from the deepest projected component first, then bubbles up to parents.
CAngular calls <code>ngAfterContentInit</code> on each component only after all projected content in the entire nested tree is initialized.
DAngular calls <code>ngAfterContentInit</code> on each component immediately after its own projected content is initialized, starting from the top-level parent down to the deepest child.
Attempts:
2 left
💡 Hint
Think about how Angular initializes content projection step by step in nested components.