How to check whether is empty? (in Angular 2+ till now)

Wrap ng-content in an HTML element like a div to get a local reference to it, then bind the ngIf expression to ref.children.length == 0:

template: `<div #ref><ng-content></ng-content></div> 
           <span *ngIf=" ! ref.children.length">
              Display this if ng-content is empty!
           </span>`

Updated for Angular 12; old logic (“ref.nativeElement.childNodes.length“) gives error, as nativeElement is undefined nowadays.

Leave a Comment