我很好奇有什么方法可以在组件中重用两次 ng-content 吗?或者将其分配给组件构造函数内的变量?
类似于:
@Component({
selector: "component"
})
@View({
template: `<ng-content></ng-content> and again <ng-content></ng-content>`
})
最佳答案
在 ng-template
的帮助下,还有另一种方法可以做到这一点
将 ng-content
包裹在 ng-template
中并使用 ngTemplateOutlet
和 ng-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>
关于angular - 是否可以在组件模板中重用 <ng-content></ng-content> ? Angular 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34461489/