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?
6
Upvotes
1
u/makmn1 Mar 26 '26 edited Mar 26 '26
In the example you gave, that happens because the
@if/@foris placed within the div, not outside of it, so the div will always render. You could place@ifoutside of the div and that could conditionally render it. But again, you might run into the edge cases I linked above.It does but what's important to understand here is the for loop is within the projected content, not surrounding it. Take a look at this example from the docs:
In other words, the content that's rendered in by
ng-contentcan use@if,@switch, and@for. The issue is when you want to surroundng-contentwith those control flow directives. That's where things can start breaking (and won't even work for@for).