Cookies concent notice

This site uses cookies from Google to deliver its services and to analyze traffic.
Learn more
Skip to main content
This site is no longer updated.Head to Angular.devHome
/

This is the archived documentation for Angular v17. Please visit angular.dev to see this page for the current version of Angular.

viewChild

Initializes a view child query.

See more...

      
      const viewChild: ViewChildFunction;
    

Description

Consider using viewChild.required for queries that should always match.

Further information is available in the Usage Notes...

Usage notes

Create a child query in your component by declaring a class field and initializing it with the viewChild() function.

      
      @Component({template: '<div #el></div><my-component #cmp />'})
export class TestComponent {
  divEl = viewChild<ElementRef>('el');                   // Signal<ElementRef|undefined>
  divElRequired = viewChild.required<ElementRef>('el');  // Signal<ElementRef>
  cmp = viewChild(MyComponent);                          // Signal<MyComponent|undefined>
  cmpRequired = viewChild.required(MyComponent);         // Signal<MyComponent>
}