Angular 使用ng模板、ng容器和;ngTemplateOutlet
我有一个可计算成本的组件,它使用ng容器来显示默认模板或传入的模板。 问题是我需要传入的模板将其上下文设置为其嵌套的组件。我尝试将ngTemplateOutlet指令上的context属性设置为包含嵌套组件的这个的对象,但是它不起作用 父组件HTML:Angular 使用ng模板、ng容器和;ngTemplateOutlet,angular,typescript,angular5,angular6,Angular,Typescript,Angular5,Angular6,我有一个可计算成本的组件,它使用ng容器来显示默认模板或传入的模板。 问题是我需要传入的模板将其上下文设置为其嵌套的组件。我尝试将ngTemplateOutlet指令上的context属性设置为包含嵌套组件的这个的对象,但是它不起作用 父组件HTML: <ng-template #didYouVisitQuestion> <div class="mt-10"> <ds-radio-buttons ...
<ng-template #didYouVisitQuestion>
<div class="mt-10">
<ds-radio-buttons
...
(onSelectRadio)="alertx()"></ds-radio-buttons>
</div>
</ng-template>
<nesting-component [questionTemplate]="didYouVisitQuestionTpl"></ds-rate-component>
@ViewChild('didYouVisitQuestion')
private didYouVisitQuestionTpl: TemplateRef<any>;
父组件类/TS:
<ng-template #didYouVisitQuestion>
<div class="mt-10">
<ds-radio-buttons
...
(onSelectRadio)="alertx()"></ds-radio-buttons>
</div>
</ng-template>
<nesting-component [questionTemplate]="didYouVisitQuestionTpl"></ds-rate-component>
@ViewChild('didYouVisitQuestion')
private didYouVisitQuestionTpl: TemplateRef<any>;
@ViewChild('didYouVisitQuestion'))
私人didYouVisitQuestionTpl:TemplateRef;
子组件HTML
<ng-template #defaultQuestion>
some default html...
</ng-template>
<ng-container *ngTemplateOutlet="questionTemplate ? questionTemplate: defaultQuestion;context:cntxt">
</ng-container>
一些默认的html。。。
子组件类/TS
@Input()questionTemplate: TemplateRef<any>;
constructor() {
this.cntxt = this;
}
alertx() {
window.alert('alertx');
}
@Input()问题模板:TemplateRef;
构造函数(){
this.cntxt=这个;
}
alertx(){
window.alert('alertx');
}
问题:
<ng-template #didYouVisitQuestion>
<div class="mt-10">
<ds-radio-buttons
...
(onSelectRadio)="alertx()"></ds-radio-buttons>
</div>
</ng-template>
<nesting-component [questionTemplate]="didYouVisitQuestionTpl"></ds-rate-component>
@ViewChild('didYouVisitQuestion')
private didYouVisitQuestionTpl: TemplateRef<any>;
我需要在嵌套组件上激活alertx()方法,当前它在父组件上激活。向ngTemplateOutlet传递此的上下文无效。
在这种情况下,如何使传入模板具有正确的上下文?请在传入范围中查找Stackblitz代码
您能分享一下stackblitz代码吗