[ Web Proxy ]
URL:
Viewing: https://angular.dev/api/core/ViewChild [Back]  [Original]

ViewChild Angular Skip to main content
menu
close
more_horiz
menuAPI
@angular/core

ViewChild

decorator

Property decorator that configures a view query. The change detector looks for the first element or the directive matching the selector in the view DOM. If the view DOM changes, and a new child matches the selector, the property is updated.

View queries

On this page

    arrow_upward_alt Back to the top

    API

    @ViewChild(
      selector: ProviderToken<unknown> | Function | string,
      opts?: { read?: any; static?: boolean },
    );

    Description

    Property decorator that configures a view query. The change detector looks for the first element or the directive matching the selector in the view DOM. If the view DOM changes, and a new child matches the selector, the property is updated.

    Metadata Properties:

    • selector - The directive type or the name used for querying.
    • read - Used to read a different token from the queried elements.
    • static - true to resolve query results before change detection runs, false to resolve after change detection. Defaults to false.

    The following selectors are supported.

    • Any class with the @Component or @Directive decorator
    • A template reference variable as a string (e.g. query <my-component #cmp></my-component> with @ViewChild('cmp'))
    • Any provider defined in the child component tree of the current component (e.g. @ViewChild(SomeService) someService: SomeService)
    • Any provider defined through a string token (e.g. @ViewChild('someToken') someTokenVal: any)
    • A TemplateRef (e.g. query <ng-template></ng-template> with @ViewChild(TemplateRef) template;)

    The following values are supported by read:

    Difference between dynamic and static queries:

    • Dynamic queries (static: false) - The query resolves before the ngAfterViewInit() callback is called. The result will be updated for changes to your view, such as changes to ngIf and ngFor blocks.
    • Static queries (static: true) - The query resolves once the view has been created, but before change detection runs (before the ngOnInit() callback is called). The result, though, will never be updated to reflect changes to your view, such as changes to ngIf and ngFor blocks.

    Usage Notes

    Example 1

    import {Component, Directive, input, signal, ViewChild} from '@angular/core';
    
    @Directive({
      selector: 'pane',
    })
    export class Pane {
      id = input.required<string>();
    }
    
    @Component({
      selector: 'example-app',
      imports: [Pane],
      template: `
        @if (shouldShow()) {
          <pane id="1" />
        } @else {
          <pane id="2" />
        }
    
        <button (click)="toggle()">Toggle</button>
    
        <div>Selected: {{ selectedPane() }}</div>
      `,
    })
    export class ViewChildComp {
      @ViewChild(Pane)
      set pane(v: Pane) {
        setTimeout(() => {
          this.selectedPane.set(v.id());
        }, 0);
      }
      selectedPane = signal('');
      shouldShow = signal(true);
      toggle() {
        this.shouldShow.update((shouldShow) => !shouldShow);
      }
    }

    Example 2

    import {AfterViewInit, Component, Directive, ViewChild} from '@angular/core';
    
    @Directive({
      selector: 'child-directive',
    })
    export class ChildDirective {}
    
    @Component({
      selector: 'someCmp',
      templateUrl: 'someCmp.html',
    })
    export class SomeCmp implements AfterViewInit {
      @ViewChild(ChildDirective) child!: ChildDirective;
    
      ngAfterViewInit() {
        // child is set
      }
    }
    Jump to details

    Web Proxy Viewer  |  New URL  |  Original Page