1 回答

TA贡献1824条经验 获得超8个赞
如果使用 ,则不能使用它来呈现此内容。您需要使用以下指令:ng-templateng-contentngTemplateOutlet
父模板:
<button (click)="show = !show">Toggle</button>
<tab [show]="show">
<ng-template>
I am rendered
</ng-template>
</tab>
儿童:
export class HelloComponent {
@ContentChild(TemplateRef)
template?: TemplateRef<any>;
@Input()
show?: boolean;
}
子模板:
<ng-container *ngIf="show" [ngTemplateOutlet]="template"></ng-container>
工作示例
另一种方法是将显示留给父组件:
父模板:
<button (click)="show = !show">Toggle</button>
<tab>
<ng-template [ngIf]="show">
I am rendered
</ng-template>
</tab>
子模板:
<ng-content></ng-content>
工作示例
最后一个答案听起来像是反对我的第一个答案,我提到你不能用它来渲染,但在这种情况下,它正在处理模板的渲染。ng-contentng-template*ngIf
添加回答
举报