import { Component,Directive,ElementRef, APP_BOOTSTRAP_LISTENER } from '@angular/core';
@Component({
selector: 'app-root',
template: `
<second-component>
test second
</second-component>
<third-component>
test third
</third-component>
`,
styles: [`
`]
})
export class AppComponent {
}
@Component({
selector: 'second-componet',
template: `
<h1>
test
</h1>
`,
styles: [`
`]
})
export class SecondCompoent{
}
@Component({
selector: 'third-componet',
template: `
<h1>
test
</h1>
`,
styles: [`
`]
})
export class ThirdCompoent{
}
component tags内部的文本,以显示在应用程序组件中。这称为content-projection,您可以使用ng-content来实现。像这样修改您的代码
import { Component,Directive,ElementRef, APP_BOOTSTRAP_LISTENER } from '@angular/core';
@Component({
selector: 'app-root',
template: `
<second-component>
test second
</second-component>
<third-component>
test third
</third-component>
`,
styles: [`
`]
})
export class AppComponent {
}
@Component({
selector: 'second-componet',
template: `
<h1>
test
</h1>
<ng-content></ng-content>
`,
styles: [`
`]
})
export class SecondCompoent{
}
@Component({
selector: 'third-componet',
template: `
<h1>
test
</h1>
<ng-content></ng-content>
`,
styles: [`
`]
})
export class ThirdCompoent{
}
注意在SecondComponent和ThirdComponent的模板中添加了。请找到示例stack-blitzng-content
有关ng-content check this的详细讨论。
谢谢。
selector: 'second-componet',
和selector: 'third-componet',