What is the purpose of the @ViewChild decorator in Angular?
The @ViewChild decorator lets you get a reference to a child component, directive, or DOM element inside your component class. It helps you interact with or control that child directly.
Click to reveal answer
beginner
How do you use @ViewChild to get a reference to a template element with a template reference variable #myDiv?
You add @ViewChild('myDiv') myDivRef: ElementRef; in your component class. This lets you access the DOM element inside myDivRef.nativeElement.
Click to reveal answer
intermediate
When is the best lifecycle hook to safely use the @ViewChild reference in Angular?
Use the ngAfterViewInit() lifecycle hook. This runs after Angular has fully initialized the view and the @ViewChild reference is ready to use.
Click to reveal answer
advanced
What does setting { static: true } or { static: false } in @ViewChild options mean?
{ static: true } means Angular resolves the reference before change detection runs (good for access in ngOnInit). { static: false } means Angular resolves it after change detection (use in ngAfterViewInit).
Click to reveal answer
intermediate
Can @ViewChild be used to get a reference to a child component? How?
Yes. You pass the child component class to <code>@ViewChild</code>, like <code>@ViewChild(ChildComponent) childCompRef: ChildComponent;</code>. This lets you call methods or access properties of the child component.
Click to reveal answer
What does @ViewChild('myInput') inputRef: ElementRef; do in an Angular component?
ACreates a new input element in the template.
BGets a reference to the DOM element with template variable <code>#myInput</code>.
CBinds a value to the input element.
DDefines a new component named myInput.
✗ Incorrect
The @ViewChild decorator with a string gets the element or directive with that template reference variable.
When can you safely access a @ViewChild reference without it being undefined?
A. Accessing @ViewChild reference before view initialization
B. Missing quotes around 'myButton' in @ViewChild
C. Using ElementRef instead of TemplateRef
D. No error, code is correct
Solution
Step 1: Check lifecycle hook used
ngOnInit runs before the view is fully initialized, so @ViewChild references are not ready yet.
Step 2: Correct lifecycle hook for @ViewChild access
Accessing @ViewChild should be done in ngAfterViewInit to ensure the element exists.
Final Answer:
Accessing @ViewChild reference before view initialization -> Option A
Quick Check:
@ViewChild ready only after ngAfterViewInit [OK]
Hint: Use ngAfterViewInit, not ngOnInit, to access @ViewChild [OK]
Common Mistakes:
Accessing @ViewChild in ngOnInit causing undefined errors
Confusing lifecycle hooks for view queries
Ignoring the timing of template element availability
5. You want to call a method focusInput() on a child component ChildComponent using @ViewChild. Which of the following is the correct way to do this safely after the view initializes?
hard
A. Use @ViewChild(ChildComponent) child: ElementRef; and call this.child.nativeElement.focusInput() inside ngAfterViewInit()
B. Use @ViewChild('child') child: ChildComponent; and call this.child.focusInput() inside constructor()
C. Use @ViewChild(ChildComponent) child!: ChildComponent; and call this.child.focusInput() inside ngAfterViewInit()
D. Use @ViewChild('child') child: ElementRef; and call this.child.focusInput() inside ngOnInit()
Solution
Step 1: Correct @ViewChild usage for child component
Use the component class type in @ViewChild to get the child component instance, e.g. @ViewChild(ChildComponent).
Step 2: Call child method after view initialization
Call the method inside ngAfterViewInit to ensure the child component is ready.
Final Answer:
Use @ViewChild(ChildComponent) child!: ChildComponent; and call this.child.focusInput() inside ngAfterViewInit() -> Option C
Quick Check:
Child component method call via @ViewChild after view init [OK]
Hint: Use component class in @ViewChild and call methods after ngAfterViewInit [OK]
Common Mistakes:
Calling child methods in constructor or ngOnInit
Using ElementRef instead of component class for child components
Trying to access nativeElement for component methods
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.