简体   繁体   English

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

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

I am curious is there any way to reuse twice ng-content in component? 我很好奇有没有办法在组件中重复使用两次ng-content Or to assign it to variable inside component constructor? 或者将它分配给组件构造函数中的变量?

Something like: 就像是:

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

There is also an other way of doing it with the help of ng-template ng-template的帮助下还有另一种方法

Wrap the ng-content inside ng-template and use ngTemplateOutlet with ng-container ng-contentng-template并将ngTemplateOutletng-container

Example: 例:

<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>

Use this as, 用这个,

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

Yes, i also found this. 是的,我也发现了这个。

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

and in component: 在组件中:

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

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

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