2
votes

I Would like to get children created with NgTemplateOutlet.

I have this example plunkr, where I have Child component and Parent Component, Parent has an input template that renders using ngTemplateOutlet:

@Component({
  selector: 'parent',
  template: `
    <div>parent</div>
    <ng-container *ngTemplateOutlet="template1"></ng-container>
    <div *ngFor="let child of children; let i = index">{{i}}</div>
    {{children.length}}    
`
})
export class Parent {
  @Input() template1; 
  children = [];
  @ViewChildren(Child) viewList: QueryList<Child>;

  updateElements() {
    setTimeout(() => this.children = this.viewList.toArray());
  }

  ngAfterViewInit() {
    this.viewList.changes.subscribe(() => this.updateElements());
    this.updateElements();
  }      
}

Parent component try to get Child children using ViewChildren but don't works.

Someone knows how to get work it?

In the other hand I have wrote other plunkr where I'm using ngFor to render several Child components and it works.

Since both, ngFor and ngTemplateOutlet, using viewContainerRef.createEmbeddedView I think two ways should work the same way.

Why not?

PS: I have using Angular 4.0.0-rc1, but I think is the same for Angular 2.x

1

1 Answers

-2
votes

In the meantime you could use workaround injecting parent into child.