How to provide a fallback for empty ng-content in Angular?
You can check for projected content yourself and show alternative content when none was found:
@Component({
template: `
<div *ngIf="hasContent">alternative content</div>
<div #wrapper>
<ng-content></ng-content>
</div>
`
})
class MyComponent implements AfterContentInit {
@ContentChild('wrapper') wrapper:ElementRef;
hasContent = false;
ngAfterContentInit() {
this.hasContent = this.wrapper.childNodes.length > 0;
}
}