首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >是否可以在组件模板中重用<ng-content></ng-content>?Angular2

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

Stack Overflow用户
提问于 2015-12-25 09:02:47
回答 2查看 4.5K关注 0票数 9

我很好奇有什么方法可以在组件中重用两次ng内容吗?还是将其分配给组件构造函数中的变量?

类似于:

代码语言:javascript
复制
@Component({
    selector: "component"
})
@View({
    template: `<ng-content></ng-content> and again <ng-content></ng-content>`
})

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-12-25 12:55:48

是的,我还找到了这个。

代码语言:javascript
复制
<my-component> <div content-a> A </div> </my-component>

在构成部分:

代码语言:javascript
复制
<ng-content select="[content-a]"></ng-content>
票数 4
EN

Stack Overflow用户

发布于 2017-08-22 06:57:00

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

ng-content封装在ng-template中,并将ngTemplateOutletng-container一起使用

示例:

代码语言:javascript
复制
<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>

用这个作为,

代码语言:javascript
复制
<my-component>Anything</my-component>
票数 17
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34461489

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档