@ViewChildren

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.

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 {
  @ViewChildren(ChildDirective) viewChildren: QueryList<ChildDirective>;

  ngAfterViewInit() {
    // viewChildren is set
  }
}
Tutorials

Options

@ContentChildren(
  selector: Type<any>|Function|string, 
  opts?: {read?: any},
)

selector#

The directive type or the name used for querying.

selector: Type<any>|Function|string

Supported selectors include:

  • 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;)

opts#

Additional options

opts?: {read?: any}
  • read - read a different token from the queried elements.