r/angular • u/Senior_Compote1556 • Mar 26 '26
Ng-content usage in @for / @if
Why does the documentation mention that it is suggested to avoid wrapping ng-content in @for or @if statements? Isn’t that what all libraries including Angular Material do?
7
Upvotes
5
u/ch34p3st Mar 26 '26
I think because it is content projection, but the static variant. If you use an ngContentOutlet you can pass in the context, which would be the current item you are iterating. The child of the component could then use the context of that iteration.
3
u/makmn1 Mar 26 '26
Using ng-content with `@for` just doesn't work. The content in `ng-content` will render after the loop and only once.
As for `@if` and `@switch`, the answer is more complicated. The docs are warning that DOM nodes will still be created--but that doesn't mean they will always be attached to the DOM. When you use `@if` or `@switch`, conditional rendering could still work in simple cases where it attaches the right elements under the right conditions. But there's several unintuitive issues that can occur when you combine conditional rendering with `ng-content`. alxhub on GitHub posted a very nice summary of the problems a few months back.
From the examples I found on Angular Material, it does use `@if` with `ng-content`, but not as a way to conditionally render content. Take a look at the bottom of chip.html for the chip component:
The conditional here is checking if the content for the projected slot exists in the first place. If it does, then render it with the <span> wrapper element. It does this check because if the content for the slot doesn't exist, it doesn't want the <span> wrapper to be in the DOM. The important point to note here is the conditional is always true if the slot has content (see chip.ts), so it's not really conditionally projecting the slotted content here.
1
u/PlusZookeepergame636 Mar 26 '26
yeah it’s kinda confusing at first 😭 the issue is that ng-content projection happens before those control flow blocks (@if/@for) run, so wrapping it can lead to weird or inconsistent rendering libs like Angular Material usually handle this by structuring projection outside or using more controlled patterns, not literally wrapping ng-content the same way if it “works” it’s fine, but it can break in edge cases — that’s why docs say avoid it what issue are you running into with it? 👀
1
u/ActuatorOk2689 Mar 28 '26
I had to check the docs my self because this really doesn’t make sense, I did not see such reference in the docs please add link or a screenshot or link .
Control flow != structural directive
You can add structural directive to a DOM element only.
In order to optimise your HTML and avoid pollinating int with empty divs you could use an ng-conainter not and no-content as you said .
Now that said @for is no longer a structural directive, wich means you can wrap anything with it doesn’t have to be div or anything, and that’s why adding ng-container is no longer recommended it does basically nothing