With angular (17 or 18)
<ng-content> must not be rendered inside an *ngIf=”false”
But content child OnInit is triggered !?
For test, I tried :
<div class="content" *ngIf="false">
<ng-content></ng-content>
</div>
And :
<app-parent>
<app-child></app-child>
</app-parent>
And for app-child :
ngOnInit() {
alert('OnInit done...');
}
Then alert is triggered, and it proves that ngOnInit is initialized even if it is not rendered because of *ngIf=”false” !!?
Tested on Angular 17 and 18 on stackblitz :
https://stackblitz.com/edit/stackblitz-starters-2tjcvw?file=src%2Fmain.ts
What do I not understand? What am I doing wrong? Is there a fix?
Or, should I report a bug to Angular team?
Hubert Blein is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.