I want to hide remove div.parent if ng-content is empty from bellow code
<div class="parent">
<ng-content></ng-content>
</div>
I tried using reference like bellow, but seems to be not working
<div class="parent" *ngIf="!child.children.length">
<div class="child">
<ng-content></ng-content>
</div>
</div>
Is there a better approach to remove parent element if ng-content is empty?
You can not reference a class like that. I think you want to reference an element, for that you would use
#on the element. But that won't help you either. Parent is rendered before child is and if parent is removed from DOM because of child changes you would stumble on an errorExpression Changed After It Has Been Checked. For that I have implemented toviewChildofparentRefin cyclengAfterContentCheckedand from that we count if child has any children (if ng-container has any elements). If it does have children we remove parent.If for some reason you want to remove parent a later time you can call
removeParent()to do so.Edit
children = [1,2,3]to observe effects of having children and not having them.Here is a working example: https://stackblitz.com/edit/angular-4krctv?file=src%2Fapp%2Fapp.component.html