0
0
Angularframework~20 mins

@ContentChild and content projection in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Content Projection Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What will be rendered by this Angular component?
Consider the following Angular component using content projection and @ContentChild. What will be displayed inside the component?
Angular
import { Component, ContentChild, AfterContentInit, ElementRef } from '@angular/core';

@Component({
  selector: 'app-wrapper',
  template: `<div>Wrapper Start</div><ng-content></ng-content><div>Wrapper End</div>`
})
export class WrapperComponent implements AfterContentInit {
  @ContentChild('projected') projectedContent!: ElementRef;

  ngAfterContentInit() {
    if (this.projectedContent) {
      this.projectedContent.nativeElement.textContent += ' (modified)';
    }
  }
}

// Usage in parent template:
// <app-wrapper>
//   <p #projected>Hello World</p>
// </app-wrapper>
AWrapper Start Hello World (modified) Wrapper End
BWrapper Start Hello World Wrapper End
CWrapper Start Wrapper End
DWrapper Start <p #projected>Hello World</p> Wrapper End
Attempts:
2 left
💡 Hint
Think about when ngAfterContentInit runs and what @ContentChild does with the template reference.
lifecycle
intermediate
1:30remaining
When is @ContentChild available in Angular component lifecycle?
In which lifecycle hook is the @ContentChild property guaranteed to be initialized and safe to use?
AngOnInit
BngAfterViewChecked
CngAfterContentInit
DngAfterViewInit
Attempts:
2 left
💡 Hint
Think about when Angular finishes projecting external content into the component.
📝 Syntax
advanced
2:00remaining
Which @ContentChild selector correctly selects a projected component instance?
Given a child component <child-comp> projected into a parent, which @ContentChild decorator correctly selects the child component instance?
Angular
import { Component, ContentChild } from '@angular/core';
import { ChildComp } from './child-comp.component';

@Component({ selector: 'parent-comp', template: `<ng-content></ng-content>` })
export class ParentComp {
  // Which @ContentChild is correct?
}
A@ContentChild(ChildComp) child!: ChildComp;
B@ContentChild('ChildComp') child!: ChildComp;
C@ContentChild('.child-comp') child!: ChildComp;
D@ContentChild('childComp') child!: ChildComp;
Attempts:
2 left
💡 Hint
Think about how to select a component class type with @ContentChild.
🔧 Debug
advanced
2:00remaining
Why does @ContentChild return undefined in this scenario?
A developer uses @ContentChild to get a projected element but finds it is undefined in ngAfterContentInit. What is the most likely cause?
Angular
import { Component, ContentChild, AfterContentInit, ElementRef } from '@angular/core';

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

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

// Usage:
// <app-wrapper>
//   <p>Hello</p>
// </app-wrapper>
AngAfterContentInit runs before content projection happens.
BThe projected element does not have the template reference variable #proj.
C@ContentChild only works with components, not native elements.
DThe selector string in @ContentChild must be a CSS class, not a template reference.
Attempts:
2 left
💡 Hint
Check the template reference variable usage in the projected content.
🧠 Conceptual
expert
2:30remaining
How does @ContentChild differ from @ViewChild in Angular?
Which statement best describes the difference between @ContentChild and @ViewChild in Angular?
A@ContentChild selects child components only, while @ViewChild selects native HTML elements only.
B@ContentChild and @ViewChild are interchangeable and select elements from the same template.
C@ContentChild works only after ngAfterViewInit, while @ViewChild works after ngAfterContentInit.
D@ContentChild selects elements projected into the component via <ng-content>, while @ViewChild selects elements declared inside the component's own template.
Attempts:
2 left
💡 Hint
Think about where the elements come from that each decorator queries.