Это действительно невозможно. @ContentChild
в компоненте просматривает содержимое внутри тега компонентов. Например: <my-cmp><ng-template #cmp_anchor></ng-template></my-cmp>
, вернет шаблон, если вы установите @ContentChild
в классе MyComponent
.
Нет прямого способа доступа к ng-template
из компонента, спроектированного с использованием ngComponentOutlet
. Вы даже не можете получить доступ к экземпляру HelloComponent
.
Вы можете сделать частный доступ к _componentRef
экземпляра ngComponentOutlet
, но это немного взломано. Я предлагаю вам вернуться к чертежной доске или перефразировать свой вопрос, указав на более серьезную проблему, которую вы пытаетесь решить с помощью своего запроса. В любом случае, чтобы он заработал, вы можете (но не должны) делать следующее:
@Component({
template: `
<div>Hello Component is here!</div>
<ng-template #cmp_anchor>
<div>Template content</div>
</ng-template>
`,
})
export class HelloComponent {
@ViewChild('cmp_anchor', {read: TemplateRef})
cmpTemplateRef: TemplateRef<any>;
}
@Component({
selector: 'my-app',
template: `
<ng-container *ngComponentOutlet="helloComponent"></ng-container>
<ng-container
[ngTemplateOutlet]="$any(ng)?._componentRef.instance.cmpTemplateRef">
</ng-container>
`
})
export class AppComponent {
public helloComponent = HelloComponent;
@ViewChild(NgComponentOutlet) ng: NgComponentOutlet;
}
рабочий пример
Если вы все еще думаете, что вам нужно это сделать, вы всегда можете написать свою собственную структурную директиву, основанную на директиве ngComponentOutlet
. Оттуда вы можете раскрыть все виды вещей из того, что передано в директиву выхода
person
Poul Kruijt
schedule
22.09.2020