Content projection lets you insert content from a parent component into a child component's template. It helps create reusable components that can display different content.
Click to reveal answer
beginner
What does the @ContentChild decorator do in Angular?
@ContentChild finds a child element or directive inside the projected content of a component. It lets the component access and interact with that projected content.
Click to reveal answer
intermediate
How do you use @ContentChild to get a reference to a projected element?
You add @ContentChild with a selector (like a template reference variable or a component/directive type) in the child component class. Angular sets that property to the matching projected element.
Click to reveal answer
intermediate
Why is @ContentChild different from @ViewChild?
@ContentChild looks inside the content projected into the component (between its tags). @ViewChild looks inside the component's own template.
Click to reveal answer
intermediate
When is the @ContentChild property available during the component lifecycle?
The @ContentChild property is available after Angular runs the ngAfterContentInit lifecycle hook. Before that, it might be undefined.
Click to reveal answer
What does content projection allow you to do in Angular?
ARun code after the view initializes
BInsert external content into a child component's template
CCreate new Angular modules
DChange the component's CSS styles dynamically
✗ Incorrect
Content projection lets you insert content from a parent into a child component's template.
Which decorator lets you access projected content inside a component?
A@ContentChild
B@ViewChild
C@Input
D@Output
✗ Incorrect
@ContentChild accesses elements inside projected content.
When is the @ContentChild property guaranteed to be set?
AngOnInit
BngAfterViewInit
CngOnDestroy
DngAfterContentInit
✗ Incorrect
@ContentChild is set after ngAfterContentInit lifecycle hook.
What is the main difference between @ContentChild and @ViewChild?
A@ContentChild accesses projected content; @ViewChild accesses component's own template
@ContentChild references are not available in ngOnInit; they become available in ngAfterContentInit.
Step 2: Accessing itemRef in ngOnInit
Accessing itemRef in ngOnInit causes it to be undefined, leading to runtime error when accessing nativeElement.
Final Answer:
Error because itemRef is undefined in ngOnInit -> Option B
Quick Check:
Use ngAfterContentInit for @ContentChild access [OK]
Hint: Access @ContentChild only in ngAfterContentInit [OK]
Common Mistakes:
Accessing @ContentChild in ngOnInit
Assuming @ContentChild is available immediately
Ignoring lifecycle hook order
5. You want to create a component that projects multiple paragraphs and highlights the first paragraph. Which approach correctly achieves this?
Options: A) Use @ContentChildren('para') and highlight the first in the list. B) Use @ContentChild('para') and add #para to the first paragraph only. C) Use @ViewChild('para') inside the component template. D) Use @ContentChild without a selector and highlight all paragraphs.
hard
A. Use @ContentChild without a selector and highlight all paragraphs.
B. Use @ContentChild('para') and add #para to the first paragraph only.
C. Use @ViewChild('para') inside the component template.
D. Use @ContentChildren('para') and highlight the first in the list.
Solution
Step 1: Understand multiple projected elements
When projecting multiple elements with the same template variable, use @ContentChildren to get all references.
Step 2: Highlight the first paragraph
@ContentChildren returns a QueryList; you can access the first element and apply highlight logic.
Final Answer:
Use @ContentChildren('para') and highlight the first in the list. -> Option D
Quick Check:
Multiple projected elements need @ContentChildren [OK]
Hint: Use @ContentChildren for multiple projected elements [OK]
Common Mistakes:
Using @ContentChild for multiple elements
Using @ViewChild for projected content
Omitting selector in @ContentChild
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.