Bird
0
0

You want to access a child component method and a DOM element inside your Angular component. Which is the correct way to use @ViewChild for both?

hard📝 component behavior Q8 of 15
Angular - Component Interaction
You want to access a child component method and a DOM element inside your Angular component. Which is the correct way to use @ViewChild for both?
AUse two separate @ViewChild decorators: one with the child component class, one with the template variable for the element
BUse a single @ViewChild with an array of selectors for both
CUse @ViewChild only for the child component and query the DOM element manually
DUse @ViewChild only for the DOM element and call the child component method via event binding
Step-by-Step Solution
Solution:
  1. Step 1: Understand @ViewChild usage for different targets

    @ViewChild can be used multiple times to get references to different elements or components separately.
  2. Step 2: Correct approach for child component and DOM element

    Use one @ViewChild with the child component class type and another with the template variable string for the DOM element.
  3. Final Answer:

    Use two separate @ViewChild decorators: one with the child component class, one with the template variable for the element -> Option A
  4. Quick Check:

    Separate @ViewChild for component and element references [OK]
Quick Trick: Use multiple @ViewChild decorators for different targets [OK]
Common Mistakes:
  • Trying to combine selectors in one @ViewChild
  • Not using component class type for child components
  • Manually querying DOM instead of @ViewChild

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes