Here's some nice and clean syntax on Angular's NgIf and using the else
statement. In short, you will declare an ElementRef on an element and then reference it in the else
block:
<div *ngIf="isLoggedIn; else loggedOut">
Welcome back, friend.
</div>
<ng-template #loggedOut>
Please friend, login.
</ng-template>
I've taken this example from NgIf, Else, Then which I found to be really well explained.
It also demonstrates using the <ng-template>
syntax:
<ng-template [ngIf]="isLoggedIn" [ngIfElse]="loggedOut">
<div>
Welcome back, friend.
</div>
</ng-template>
<ng-template #loggedOut>
<div>
Please friend, login.
</div>
</ng-template>
And also using <ng-container>
if that's what you're after:
<ng-container
*ngIf="isLoggedIn; then loggedIn; else loggedOut">
</ng-container>
<ng-template #loggedIn>
<div>
Welcome back, friend.
</div>
</ng-template>
<ng-template #loggedOut>
<div>
Please friend, login.
</div>
</ng-template>
Source is taken from here on Angular's NgIf and Else syntax.