无法从父级访问子组件

时间:2016-06-27 20:05:11

标签: typescript angular

我无法从父级访问子组件。以下代码为数组长度输出0我预计3个孩子?此外,myChildren observable从未收到更改事件;因此,我不认为这是一个时间问题。

Plunker

import {Component, ContentChildren, QueryList, ngAfterContentInit} from '@angular/core';
import {ChildComponent} from './child'

@Component({
  selector: 'parent-component',
    template: `
    <div class='box-container'>
      <p>I'm the parent</p>
      <child-component></child-component>
      <child-component></child-component>
      <child-component></child-component>
    </div>
  `,
  directives: [ChildComponent]
})
export class ParentComponent implements AfterContentInit  {
  @ContentChildren(ChildComponent, {descendants: true}) myChildren: QueryList<ChildComponent>;

  ngAfterContentInit( ) {
    console.log(this.myChildren.toArray().length)
    this.myChildren.changes.subscribe(_ => {
      console.log(myChildren.length);
    });

  }
}

0 个答案:

没有答案