0
0
Angularframework~5 mins

@ViewChild decorator usage in Angular - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
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.
When can you safely access a @ViewChild reference without it being undefined?
AInside <code>ngOnInit()</code> always.
BBefore the component loads.
CInside <code>ngAfterViewInit()</code> lifecycle hook.
DInside the constructor.
What does the { static: true } option in @ViewChild do?
AResolves the reference before change detection runs.
BPrevents the reference from being set.
CMakes the reference read-only.
DDelays the reference resolution until after view init.
Can @ViewChild be used to access a child component's methods?
AYes, by passing the child component class to <code>@ViewChild</code>.
BNo, it only works with DOM elements.
COnly if the child component is in a different module.
DOnly if the child component is a directive.
What type is commonly used with @ViewChild to access native DOM elements?
AComponentRef
BTemplateRef
CViewContainerRef
DElementRef
Explain how to use @ViewChild to get a reference to a DOM element and when to access it safely.
Think about template variables and Angular lifecycle timing.
You got /4 concepts.
    Describe the difference between { static: true } and { static: false } options in @ViewChild and when to use each.
    Consider when Angular sets the view references.
    You got /4 concepts.