假设我有一个组件:
@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 {
}
现在,如果此组件的 <ng-content>
为空,我想显示一些默认内容。有没有直接访问 DOM 的简单方法?
原文由 Slawomir Dadas 发布,翻译遵循 CC BY-SA 4.0 许可协议
Wrap
ng-content
in an HTML element like adiv
to get a local reference to it, then bind thengIf
expression toref.children.length == 0
: