Why does [ngIf] on a template not apply when plugged into an outlet?

Discussão em 'Angular' iniciado por 1p0d, Agosto 26, 2025.

  1. 1p0d

    1p0d Guest

    To clarify, I am aware of the deprecation status on the NgIf directive, but am still interested in finding an answer for the issue. In my example, the [ngIf] does not apply when used with an outlet thus the template (top right text) is wrongfully rendering. I would like to know if this might be a bug or a syntax misuse and if there is another way than replacing [ngIf] with *ngIf, which would resolve this.

    a.component.html

    <app-b>
    <ng-template #topRightText [ngIf]="false">
    <span>Top right text</span>
    </ng-template>
    </app-b>


    b.component.ts

    @ContentChild('topRightText') topRightTextTemplateRef: TemplateRef<any>;


    b.component.html

    <ng-container [ngTemplateOutlet]="topRightTextTemplateRef"></ng-container>


    I expected that [ngIf], the long-hand syntax of *ngIf, applies the same when applied on a template that is used in an outlet. When replacing [ngIf] with *ngIf, the boolean is correctly used for conditional rendering, but why? Isn't [ngIf] just the long-hand syntax equivalent? Like explained here: https://angular.dev/api/common/NgIf#shorthand-syntax

    Continue reading...

Compartilhe esta Página