填写这份《一分钟调查》,帮我们(开发组)做得更好!去填写Home

ViewChild

属性装饰器,用于配置一个视图查询。 变更检测器会在视图的 DOM 中查找能匹配上该选择器的第一个元素或指令。 如果视图的 DOM 发生了变化,出现了匹配该选择器的新的子节点,该属性就会被更新。

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.

查看"说明"...

说明

在调用 NgAfterViewInit 回调函数之前就会设置这些视图查询。

View queries are set before the ngAfterViewInit callback is called.

元数据属性

Metadata Properties:

  • selector - 用于查询的指令类型或名字。

    selector - The directive type or the name used for querying.

  • read - 从查询到的元素中读取另一个令牌。

    read - Used to read a different token from the queried elements.

  • static - 如果为 true,则在变更检测运行之前解析查询结果,如果为 false,则在变更检测之后解析。默认为 false。

    static - True to resolve query results before change detection runs, false to resolve after change detection. Defaults to false.

支持下列选择器:

The following selectors are supported.

  • 任何带有 @Component@Directive 装饰器的类

    Any class with the @Component or @Directive decorator

  • 字符串形式的模板引用变量(比如可以使用 @ViewChild('cmp') 来查询 <my-component #cmp></my-component>

    A template reference variable as a string (e.g. query <my-component #cmp></my-component> with @ViewChild('cmp'))

  • 组件树中任何当前组件的子组件所定义的提供者(比如 @ViewChild(SomeService) someService: SomeService

    Any provider defined in the child component tree of the current component (e.g. @ViewChild(SomeService) someService: SomeService)

  • 任何通过字符串令牌定义的提供者(比如 @ViewChild('someToken') someTokenVal: any

    Any provider defined through a string token (e.g. @ViewChild('someToken') someTokenVal: any)

  • TemplateRef(比如可以用 @ViewChild(TemplateRef) template; 来查询 <ng-template></ng-template>

    A TemplateRef (e.g. query <ng-template></ng-template> with @ViewChild(TemplateRef) template;)

选项

使用说明

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

@Directive({selector: 'pane'})
export class Pane {
  @Input() id!: string;
}

@Component({
  selector: 'example-app',
  template: `
    <pane id="1" *ngIf="shouldShow"></pane>
    <pane id="2" *ngIf="!shouldShow"></pane>

    <button (click)="toggle()">Toggle</button>

    <div>Selected: {{selectedPane}}</div>
  `,
})
export class ViewChildComp {
  @ViewChild(Pane)
  set pane(v: Pane) {
    setTimeout(() => {
      this.selectedPane = v.id;
    }, 0);
  }
  selectedPane: string = '';
  shouldShow = true;
  toggle() {
    this.shouldShow = !this.shouldShow;
  }
}
    

例子

Example 2

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

@Directive({selector: 'child-directive'})
class ChildDirective {
}

@Component({selector: 'someCmp', templateUrl: 'someCmp.html'})
class SomeCmp implements AfterViewInit {
  @ViewChild(ChildDirective) child!: ChildDirective;

  ngAfterViewInit() {
    // child is set
  }
}