我有一个接受templateRef的容器组件,并且在该容器组件内,我想访问一个嵌入在模板内部的组件(预定义)。在这里,我不能使用ContentChild,因为它不是通过内容来的。
//Container Component
@Component({
selector: 'app-cnt',
template: `<div>
content goes here({{name}}):-
<ng-container [ngTemplateOutlet]="template"></ng-container>
</div>`,
styles: [`h1 { font-family: Lato; }`]
})
export class ContainerComponent {
@Input() name: string;
@Input() template: TemplateRef<any>;
//In this component i need to access Test component which comes in template
}
//Container component usage
<app-cnt [template]="templ" name="{{ name }}"></app-cnt>
<ng-template #templ>
<span style="color: green">Embed Test comonent</span>
<app-test class="app-test"></app-test>
</ng-template>