Spec-Zone.ru › Angular 2

Декоратор ContentChildren

Что он делает

Настраивает запрос к содержимому.

Как использовать

.

import {AfterContentInit, ContentChildren, Directive, QueryList} from '@angular/core';

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

@Directive({selector: 'someDir'})
class SomeDir implements AfterContentInit {
  @ContentChildren(ChildDirective) contentChildren: QueryList<ChildDirective>;

  ngAfterContentInit() {
    // contentChildren is set
  }
}

Описание

Вы можете использовать ContentChildren, чтобы получить QueryList элементов или директив из DOM содержимого. Всякий раз, когда элемент-потомок добавляется, удаляется или перемещается, список запросов обновляется, и наблюдаемые изменения списка запросов издают новое значение.

Запросы к содержимому устанавливаются до вызова обратного вызова ngAfterContentInit.

Свойства метаданных:

  • selector - тип директивы или имя, используемое для запроса.
  • descendants - включать только непосредственных потомков или всех потомков.
  • read - прочитать другой токен из запрошенных элементов.

Давайте посмотрим на пример:

import {Component, ContentChildren, Directive, Input, QueryList} from '@angular/core';

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

@Component({
  selector: 'tab',
  template: `
    <div>panes: {{serializedPanes}}</div> 
  `
})
export class Tab {
  @ContentChildren(Pane) panes: QueryList<Pane>;

  get serializedPanes(): string { return this.panes ? this.panes.map(p => p.id).join(', ') : ''; }
}

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

    <button (click)="show()">Show 3</button>
  `,
})
export class ContentChildrenComp {
  shouldShow = false;

  show() { this.shouldShow = true; }
}

пакет npm: @angular/core

экспортировано из @angular/core/index, определено в @angular/core/src/metadata/di.ts

© 2010–2017 Google, Inc.
Licensed under the Creative Commons Attribution License 4.0.
https://v2.angular.io/docs/ts/latest/api/core/index/ContentChildren-decorator.html

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API