Этого можно достичь, не назначая динамическую ссылку на шаблон [#cp1, #cp2 ...]
также.
В вашем .html
<div #container>
<ng-container #main></ng-container>
</div>
В вашем .ts
@ViewChild('main', {read: ViewContainerRef}) vcr: ViewContainerRef;
@ViewChild('container') container: ElementRef;
constructor(private cfr: ComponentFactoryResolver,
private el: ElementRef) {}
ngAfterViewInit() {
for(let i=0; i<3; i++) {
this.vcr.createComponent(this.cfr.resolveComponentFactory(MyComponent));
}
}
Теперь доступ ваш другой компонент
console.log(this.container.nativeElement.childNodes[1] //childNodes[0|1|2]
Таким образом, вы можете оценить все функциональные возможности ElementRef
, как ...childNodes[0].innerHTML