假设我有一个组件:
@Component({
selector: 'MyContainer',
template: `
<div class="container">
<!-- some html skipped -->
<ng-content></ng-content>
<span *ngIf="????">Display this if ng-content is empty!</span>
<!-- some html skipped -->
</div>`
})
export class MyContainer {
}
Run Code Online (Sandbox Code Playgroud)
现在,如果<ng-content>此组件为空,我想显示一些默认内容.有没有直接访问DOM的简单方法?
我想提供仅在内容未被转换时才会出现的默认内容.
例如,这是我的组件模板:
<article>
<header>
<ng-content select="[header]"></ng-content>
</header>
<section>
<ng-content></ng-content>
</section>
</article>
Run Code Online (Sandbox Code Playgroud)
我可以像这样使用它:
<my-component>
<h1 header>This is my header</h1>
<p>This is my content</p>
</my-component>
Run Code Online (Sandbox Code Playgroud)
现在如果我想提供一个默认标题怎么办?可能吗; 没有杂技,比如检查内容ngAfterContentInit?