Bird
0
0

How do you correctly use @ContentChild to get a reference to a projected element with the template reference variable contentRef?

easy📝 Syntax Q3 of 15
Angular - Component Interaction
How do you correctly use @ContentChild to get a reference to a projected element with the template reference variable contentRef?
A@ContentChild(contentRef) contentElement!: ElementRef;
B@ContentChild('contentRef') contentElement!: ElementRef;
C@ContentChild(#contentRef) contentElement!: ElementRef;
D@ContentChild('contentRef', {static: false}) contentElement: string;
Step-by-Step Solution
Solution:
  1. Step 1: Syntax of @ContentChild

    The decorator takes a string selector matching the template reference variable name as a string.
  2. Step 2: Correct usage

    Use @ContentChild('contentRef') with quotes around the variable name and declare the property type, usually ElementRef or a directive.
  3. Final Answer:

    @ContentChild('contentRef') contentElement!: ElementRef; -> Option B
  4. Quick Check:

    Template refs are passed as strings without # in @ContentChild. [OK]
Quick Trick: Use string name without # in @ContentChild selector [OK]
Common Mistakes:
  • Omitting quotes around the template reference variable name.
  • Including the # symbol inside the selector string.
  • Assigning wrong property types like string instead of ElementRef.

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes