I'm facing some challenges with Angular content projection.
I have a template that has some common HTML for every instance that will be used, and another part that will be subject to change from case to case, via transclusion (ng-content
).
The problem is that no matter the order I use on my template DOM elements , the output is always the same.
Here's the code:
<ng-template #lol>
<ng-content select=".world"></ng-content>
<div>Hello</div>
</ng-template>
<ng-container [ngTemplateOutlet]="lol">
<div class="world">World</div>
</ng-container>
I would expect the produced result to be:
World
Hello
given that I'm placing first the transcluded element and only then the static part of the template. But even if I switch their order on the template, the result will always be:
Hello
World
And I can't understand why. Can someone please shed some lights on why this is happening and what can I do to produce the output I want? Thank you.
NOTE: Here's a StackBlitz with a full example: https://stackblitz.com/edit/angular-vfecbs?file=src%2Fapp%2Fapp.component.html
You probably are not using it correctly. Suppose that you're using the template code that you've specified in the OP in a Component named
HelloComponent
.It should then be used like this:
HEre's a Sample StackBlitz for your ref.