繁体   English   中英

是否可以重复使用 <ng-content></ng-content> 在组件模板中? Angular2

[英]Is it possible to reuse <ng-content></ng-content> in component template? Angular2

我很好奇有没有办法在组件中重复使用两次ng-content 或者将它分配给组件构造函数中的变量?

就像是:

 @Component({ selector: "component" }) @View({ template: `<ng-content></ng-content> and again <ng-content></ng-content>` }) 

ng-template的帮助下还有另一种方法

ng-contentng-template并将ngTemplateOutletng-container

例:

<ng-container *ngIf="YourCondition" *ngTemplateOutlet="content"></ng-container>
<ng-container *ngIf="!YourCondition" *ngTemplateOutlet="content"></ng-container>

<ng-template #content><ng-content></ng-content></ng-template>

用这个,

<my-component>Anything</my-component>

是的,我也发现了这个。

<my-component> <div content-a> A </div> </my-component>

在组件中:

<ng-content select="[content-a]"></ng-content>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM