Giving the following structure:
root:
<parent>
<child>1st child content</child>
<child>2nd child content</child>
</parent>
parent.html:
<ng-container *ngTemplateOutlet="children[currentChildIndex]"></ng-container><br/>
parent.ts:
export class ParentComponent implements OnInit {
@ContentChildren(ChildComponent, { read: TemplateRef })
set childrenQueryList(val: QueryList<ChildComponent>) {
this.children = val.toArray();
}
children;
currentChildIndex = 0
child.html:
<ng-content></ng-content>
child.ts:
public foo() {
alert(`foo`)
}
when I'm trying to call a child method from the parent like:
this.children[0].foo()
I'm getting
Cannot read property 'foo' of undefined
and, of course, the children aren't showing.