Bird
0
0

You want to create a component that projects multiple paragraphs and highlights the first paragraph. Which approach correctly achieves this?

hard📝 Application Q15 of 15
Angular - Component Interaction
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.
AUse @ContentChild without a selector and highlight all paragraphs.
BUse @ContentChild('para') and add #para to the first paragraph only.
CUse @ViewChild('para') inside the component template.
DUse @ContentChildren('para') and highlight the first in the list.
Step-by-Step Solution
Solution:
  1. Step 1: Understand multiple projected elements

    When projecting multiple elements with the same template variable, use @ContentChildren to get all references.
  2. Step 2: Highlight the first paragraph

    @ContentChildren returns a QueryList; you can access the first element and apply highlight logic.
  3. Final Answer:

    Use @ContentChildren('para') and highlight the first in the list. -> Option D
  4. Quick Check:

    Multiple projected elements need @ContentChildren [OK]
Quick Trick: Use @ContentChildren for multiple projected elements [OK]
Common Mistakes:
  • Using @ContentChild for multiple elements
  • Using @ViewChild for projected content
  • Omitting selector in @ContentChild

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes