T
The Daily Insight

What is ViewChildren in angular

Author

Mia Kelly

Published Apr 15, 2026

The ViewChild or ViewChildren decorators are used to Query and get the reference of the DOM element in the Component. ViewChild returns the first matching element and ViewChildren returns all the matching elements as a QueryList of items. We can use these references to manipulate element properties in the component.

What is the use of ViewChildren in angular?

The ViewChild or ViewChildren decorators are used to Query and get the reference of the DOM element in the Component. ViewChild returns the first matching element and ViewChildren returns all the matching elements as a QueryList of items. We can use these references to manipulate element properties in the component.

What is ViewChildren?

A ViewChild is a component, directive, or element as a part of a template. If we want to access a child component, directive, DOM element inside the parent component, we use the decorator @ViewChild() in Angular. … Since the child component can be located inside the parent component, it can accessed as @ViewChild.

What is ViewChild and ViewChildren in angular?

Both ViewChild and ViewChildren are used to communicate between the components to access the data. @ViewChild and @ViewChildren are the types of decorators used to access the child component class and its different properties into the parent component. It’s similar to the inheritance.

Why do we use ViewChild?

The viewchild can get be used to get reference of template elements, so you can see all the associated attributes. A common example would be if there were to be a custom component in a template a ViewChild could be used to pull values out of that component when needed.

What is ViewChild and ContentChild?

ViewChild is used to select an element from component’s template while ContentChild is used to select projected content.

What is ContentChild and ContentChildren?

The ContentChild & ContentChildren are decorators, which we use to Query and get the reference to the Projected Content in the DOM. Projected content is the content that this component receives from a parent component. The ContentChild & ContentChildren is very similar to the ViewChild & ViewChildren.

What is TemplateRef in angular?

TemplateRef is a class and the way to reference the ng-template in the component or directive class. Using the TemplateRef we can manipulate the template from component code.

What is the decorator in angular?

Decorators are a design pattern that is used to separate modification or decoration of a class without modifying the original source code. In AngularJS, decorators are functions that allow a service, directive or filter to be modified prior to its usage.

What is content child?

ContentChildren is a parameter decorator that is used to fetch the QueryList of elements or directives from the content DOM. The QueryList is updated whenever the child element/component is added or removed.

Article first time published on

What is @input in Angular?

A common pattern in Angular is sharing data between a parent component and one or more child components. … @Input() lets a parent component update data in the child component.

What is QueryList in Angular?

The QueryList is unmodifiable list of items that Angular keeps up-to-date when the state of the application changes. The ViewChildren and ContentChildren uses QueryList to store elements or directives from view DOM and content DOM respectively.

What is ElementRef in Angular?

According to the official docs. Angular ElementRef is a wrapper around a native element inside of a View. It’s simply a class that wraps native DOM elements in the browser and allows you to work with the DOM by providing the nativeElement object which exposes all the methods and properties of the native elements.

What is static in ViewChild?

The static option for @ViewChild() and @ContentChild() queries determines when the query results become available. With static queries (static: true), the query resolves once the view has been created, but before change detection runs.

Why do we use @ViewChild in angular2?

The ViewChild decorator is used to gain access to a child component, found in the template, so that you can access its properties and methods.

What is AfterViewInit in Angular?

AfterViewInit is called when the component’s view has been attached. Remember that Angular compiles all views to JS files, not html – the framework manages templates in code and has a rendering engine to interact with the DOM.

What is Contentprojection?

Content projection is a pattern in which you insert, or project, the content you want to use inside another component. For example, you could have a Card component that accepts content provided by another component.

What is difference between component and directive?

Component is used to break up the application into smaller components. But Directive is used to design re-usable components, which is more behavior-oriented. That is why components are widely used in later versions of Angular to make things easy and build a total component-based model.

What is Shadow DOM in Angular?

Shadow DOM is like a parallel DOM tree hosted inside a component (an HTML element, not to be confused with Angular components), hidden away from the main DOM tree. No part of the application has access to this shadow DOM other than the component itself.

How do I use ngAfterContentInit?

When should you use ngAfterContentInit? Use ngAfterContentInit to call something once after all of the content has been initialized. ngAfterContentInit will run once after the first ngDoCheck().

Why Angular is called Spa?

So when you load the application for the first time, not all the pages from the server will be rendered… It’s only index.html that loads when you load the application. Since only a single page is loaded it is called SPA.

What is injectable in Angular?

The @Injectable() decorator specifies that Angular can use this class in the DI system. The metadata, providedIn: ‘root’ , means that the HeroService is visible throughout the application. … If you define the component before the service, Angular returns a run-time null reference error.

How observables are used?

Angular makes use of observables as an interface to handle a variety of common asynchronous operations. The HTTP module uses observables to handle AJAX requests and responses. … The Router and Forms modules use observables to listen for and respond to user-input events.

How do I use TemplateRef?

Access a TemplateRef instance by placing a directive on an <ng-template> element (or directive prefixed with * ). The TemplateRef for the embedded view is injected into the constructor of the directive, using the TemplateRef token.

What is ViewChild decorator in Angular?

The @ViewChild decorator allows us to inject into a component class references to elements used inside its template, that’s what we should use it for. Using @ViewChild we can easily inject components, directives or plain DOM elements.

What is the difference between ng container and ng-template?

To sum up, ng-content is used to display children in a template, ng-container is used as a non-rendered container to avoid having to add a span or a div, and ng-template allows you to group some content that is not rendered directly but can be used in other places of your template or you code.

What is Ng-content directive in Angular?

The ng-content tag is used for content projection. It is basically a placeholder to hold the dynamic content until it is parsed. Once the template is parsed, Angular replaces the tag with content.

What are directives in Angular?

Directives are classes that add additional behavior to elements in your Angular applications. Use Angular’s built-in directives to manage forms, lists, styles, and what users see. … Attribute directives—directives that change the appearance or behavior of an element, component, or another directive.

What is @input and @output Angular?

@Input() and @Output() allow Angular to share data between the parent context and child directives or components. An @Input() property is writable while an @Output() property is observable.

What is @input and @output in Angular 2?

The @Input is a decorator to mark an input property. The @Output is a decorator to mark an output property. The @Input is used to define an input property to achieve component property binding. The @Output is used to define output property to achieve custom event binding.

What is lazy loading in Angular?

Lazy loading is a technology of angular that allows you to load JavaScript components when a specific route is activated. It improves application load time speed by splitting the application into many bundles. When the user navigates by the app, bundles are loaded as needed.