ViewChildren

Configures a view query.

查看"说明"...

说明

You can use ViewChildren to get the QueryList of elements or directives from the view DOM. Any time a child element is added, removed, or moved, the query list will be updated, and the changes observable of the query list will emit a new value.

View queries are set before the ngAfterViewInit callback is called.

Metadata Properties:

  • selector - the directive type or the name used for querying.
  • read - read a different token from the queried elements.

选项

使用说明

Example

import {AfterViewInit, Component, Directive, QueryList, ViewChildren} from '@angular/core'; @Directive({selector: 'child-directive'}) class ChildDirective { } @Component({selector: 'someCmp', templateUrl: 'someCmp.html'}) class SomeCmp implements AfterViewInit { // TODO(issue/24571): remove '!'. @ViewChildren(ChildDirective) viewChildren !: QueryList<ChildDirective>; ngAfterViewInit() { // viewChildren is set } }
      
      
  1. import {AfterViewInit, Component, Directive, QueryList, ViewChildren} from '@angular/core';
  2.  
  3. @Directive({selector: 'child-directive'})
  4. class ChildDirective {
  5. }
  6.  
  7. @Component({selector: 'someCmp', templateUrl: 'someCmp.html'})
  8. class SomeCmp implements AfterViewInit {
  9. // TODO(issue/24571): remove '!'.
  10. @ViewChildren(ChildDirective) viewChildren !: QueryList<ChildDirective>;
  11.  
  12. ngAfterViewInit() {
  13. // viewChildren is set
  14. }
  15. }

Example

import {AfterViewInit, Component, Directive, Input, QueryList, ViewChildren} from '@angular/core'; @Directive({selector: 'pane'}) export class Pane { // TODO(issue/24571): remove '!'. @Input() id !: string; } @Component({ selector: 'example-app', template: ` <pane id="1"></pane> <pane id="2"></pane> <pane id="3" *ngIf="shouldShow"></pane> <button (click)="show()">Show 3</button> <div>panes: {{serializedPanes}}</div> `, }) export class ViewChildrenComp implements AfterViewInit { // TODO(issue/24571): remove '!'. @ViewChildren(Pane) panes !: QueryList<Pane>; serializedPanes: string = ''; shouldShow = false; show() { this.shouldShow = true; } ngAfterViewInit() { this.calculateSerializedPanes(); this.panes.changes.subscribe((r) => { this.calculateSerializedPanes(); }); } calculateSerializedPanes() { setTimeout(() => { this.serializedPanes = this.panes.map(p => p.id).join(', '); }, 0); } }
      
      
  1. import {AfterViewInit, Component, Directive, Input, QueryList, ViewChildren} from '@angular/core';
  2.  
  3. @Directive({selector: 'pane'})
  4. export class Pane {
  5. // TODO(issue/24571): remove '!'.
  6. @Input() id !: string;
  7. }
  8.  
  9. @Component({
  10. selector: 'example-app',
  11. template: `
  12. <pane id="1"></pane>
  13. <pane id="2"></pane>
  14. <pane id="3" *ngIf="shouldShow"></pane>
  15. <button (click)="show()">Show 3</button>
  16. <div>panes: {{serializedPanes}}</div>
  17. `,
  18. })
  19. export class ViewChildrenComp implements AfterViewInit {
  20. // TODO(issue/24571): remove '!'.
  21. @ViewChildren(Pane) panes !: QueryList<Pane>;
  22. serializedPanes: string = '';
  23.  
  24. shouldShow = false;
  25.  
  26. show() { this.shouldShow = true; }
  27.  
  28. ngAfterViewInit() {
  29. this.calculateSerializedPanes();
  30. this.panes.changes.subscribe((r) => { this.calculateSerializedPanes(); });
  31. }
  32.  
  33. calculateSerializedPanes() {
  34. setTimeout(() => { this.serializedPanes = this.panes.map(p => p.id).join(', '); }, 0);
  35. }
  36. }