【发布时间】:2016-06-15 07:38:04
【问题描述】:
我想创建表示基本面板的组件(使用 HTML 模板)。然后我想继承这个基类,但只指定标题(作为类属性/函数或@Input)和内容(作为@Component 装饰器中的template 或templateUrl)。
@Component({
template: `
<div class="panel">
<div class="title">{{title}}</idv>
<div class="content">???</div>
</idv>
`
})
export class BasePanel {
@Input()
protected title: string = '';
}
@Component({
selector: 'my-panel',
template: `MyPanel template - lorem ipsum`
})
export class MyPanel extends BasePanel {
title: string = 'MyPanel title';
}
然后在其他模板中:
<my-panel></my-panel>
或
<my-panel title="My better panel"></my-panel>
非常好的解决方案是在 HTML 标记之间传递内容:
<my-panel title="My panel with content">
<p>Lorem ipsum dolor sit amet,</p>
<p>consectetur adipiscing elit.</p>
</my-panel>
编辑 1
@rinukkusu 提供的带有 ng-content 的解决方案正在发挥作用。
但我希望有可能创建 BasePanel 组件来装饰派生组件模板。
【问题讨论】:
-
我不知道“哪个会装饰派生组件模板”。方法。你能详细说明一下吗?
-
@GünterZöchbauer 在一个组件中,我只想将 HTML 用于面板内容(带有变量等),这将通过 HTML 声明填充、边距、边框等进行装饰 - 对所有面板进行相同的装饰应用。这种装饰最好由
BasePanel完成——但这种想法来自PHP。 -
听起来像
<ng-content>下面解释是正确的使用方法。
标签: angular