因此,在 Angular2 中,以下内容非常简单:
@Component({
selector: 'some',
properties: ['header']
})
@View({
template: `
<div>
<h2>{{ getFormattedHeader() }}</h2>
<p><content></content></p>
</div>
`
})
class SomeComponent {
header: string;
getFormattedHeader() {
return this.header + '!';
}
}
<some header="Header Text">Content</some>
你得到这个:
<div>
<h2>Header Text!</h2>
<p>Content</p>
</div>
但是,如果我想对内容应用格式怎么办?我可以写一个getFormattedContent()
函数,如果可以,我用什么代替this.header
?
就此而言,我本可以选择format(header)
在模板中使用一个format
方法,该方法接受一个字符串并以!
. 有什么我可以在模板中放入类似于的东西format(
<content></content>
)
吗?显然,我的format
方法需要稍微复杂一些,因为<content></content>
它不是字符串,但只要我知道它的类型(ElementCollection
??NodeList
),这并不是一个特别重要的问题。
显然,有一种解决方法,只需将属性中的所有内容都推入并将内容留空,但我发现这很难看(特别是因为显然无法定义不需要关闭的标签)。