Javascript 如何在Angular中渲染开槽ng模板
我有自定义组件Javascript 如何在Angular中渲染开槽ng模板,javascript,angular,Javascript,Angular,我有自定义组件选项卡。此选项卡组件的模板如下所示: <ng-content></ng-content> 在选项卡组件中,我有一个输入参数。根据这个输入参数,我希望这个有槽的ng模板被渲染或不被渲染。我如何实现这一点?如何从选项卡组件内部呈现此ng模板?如果使用ng模板,则不能使用ng内容呈现此模板。您需要使用ngTemplateOutlet指令: 父模板: <button (click)="show = !show">Toggle</button>
选项卡
。此选项卡
组件的模板如下所示:
<ng-content></ng-content>
在
选项卡
组件中,我有一个输入
参数。根据这个输入
参数,我希望这个有槽的ng模板
被渲染或不被渲染。我如何实现这一点?如何从选项卡
组件内部呈现此ng模板
?如果使用ng模板
,则不能使用ng内容
呈现此模板。您需要使用ngTemplateOutlet
指令:
父模板:
<button (click)="show = !show">Toggle</button>
<tab [show]="show">
<ng-template>
I am rendered
</ng-template>
</tab>
<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>
切换
我很高兴
儿童ts:
export class HelloComponent {
@ContentChild(TemplateRef)
template?: TemplateRef<any>;
@Input()
show?: boolean;
}
导出类HelloComponent{
@ContentChild(TemplateRef)
模板?:TemplateRef
另一种方法是将显示留给父组件:
父模板:
<button (click)="show = !show">Toggle</button>
<tab [show]="show">
<ng-template>
I am rendered
</ng-template>
</tab>
<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>
切换
我很高兴
子模板:
<button (click)="show = !show">Toggle</button>
<tab [show]="show">
<ng-template>
I am rendered
</ng-template>
</tab>
<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 content
来呈现ng模板
,但在这种情况下,*ngIf
负责模板的呈现。如果您使用ng template
,您不能使用ng content
来呈现是。您需要使用ngTemplateOutlet
指令:
父模板:
<button (click)="show = !show">Toggle</button>
<tab [show]="show">
<ng-template>
I am rendered
</ng-template>
</tab>
<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>
切换
我很高兴
儿童ts:
export class HelloComponent {
@ContentChild(TemplateRef)
template?: TemplateRef<any>;
@Input()
show?: boolean;
}
导出类HelloComponent{
@ContentChild(TemplateRef)
模板?:TemplateRef
另一种方法是将显示留给父组件:
父模板:
<button (click)="show = !show">Toggle</button>
<tab [show]="show">
<ng-template>
I am rendered
</ng-template>
</tab>
<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>
切换
我很高兴
子模板:
<button (click)="show = !show">Toggle</button>
<tab [show]="show">
<ng-template>
I am rendered
</ng-template>
</tab>
<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 content
来呈现ng模板
,但在这种情况下,*ngIf
负责模板的呈现。如果输入参数被称为show
,您是否尝试过
?是的,但我尝试过不工作如果输入参数名为“显示”
,您是否尝试过
?是的,我尝试过,但不工作