I am working on building a Header component in Angular and want to show the navigation at different locations in the DOM structure according to whether an input inputTwoRows
has been set to true/false
. The nav is added to the Header's template via content projection - ng-content
. I have tried to wrap 2 ng-content
in ng-templates
at different locations in the template and added ngIf
to conditionally show them. The templates are, however, attempting to show the same projected content. As you will see in the Stackblitz link demoing the issue only the first ng-content
is shown if [twoRows]="true"
on c-header
. Below is the code for header.component.html
:
<header>
<div>logo</div>
<ng-template [ngIf]="inputTwoRows">
<p>Two Rows</p>
<ng-content select="c-header-nav"></ng-content>
</ng-template>
<div>utils</div>
<ng-template [ngIf]="!inputTwoRows">
<p>One Row</p>
<ng-content select="c-header-nav"></ng-content>
</ng-template>
</header>
This logic works fine if the content inside either ng-template
is not ng-content
. Is there a way I can achieve my original aim somehow?
https://stackblitz.com/edit/angular-ivy-xfr7hs?file=src/app/components/header/header.component.html
Thanks
James