Skip to main content

What is ViewChild and why is it used?

Entry Angular
Quick Answer ViewChild queries a single child element, component, or directive from the view. @ViewChild(ChildComponent) child: ChildComponent gives you a reference to access the child's public methods and properties. Available after ngAfterViewInit. ContentChild queries projected content. Use for: accessing template reference variables, controlling child component behavior, integrating third-party libraries that need DOM references.

Answer

ViewChild allows a component to access child components, DOM elements, or directives.

It is useful for calling child methods, reading template references, and interacting with the DOM after view initialization.

S
SugharaIQ Editorial Team Verified Answer

This answer has been peer-reviewed by industry experts holding senior engineering roles to ensure technical accuracy and relevance for modern interview standards.

Want to bookmark, take notes, or join discussions?

Sign in to access all features and personalize your learning experience.

Sign In Create Account

Source: SugharaIQ

Ready to level up? Start Practice