How can I use *ngIf else?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In Angular templates, *ngIf conditionally renders part of the DOM, and the else clause lets you render an alternate template when the condition is false. It is a clean way to switch between loading, empty, and ready states without filling the component class with manual DOM logic.
Basic *ngIf else Syntax
The classic pattern uses an element with *ngIf and an ng-template for the fallback:
If user is truthy, Angular renders the div. If not, Angular renders the guestTemplate.
The key idea is that else does not point to another component. It points to a named template reference.
Practical UI States
*ngIf else becomes most useful when a view has two explicit states such as:
- logged in or logged out
- data loaded or still loading
- item exists or item missing
A loading example is a common pattern:
This keeps the template readable because the alternate view is named and isolated.
Using then and else Together
Angular also supports an explicit then and else form. That can be helpful when both branches are substantial and you want neither one inline.
This style is useful when the true branch is large enough that inlining it would clutter the template.
Keep Logic in the Component, Not the Template
A common anti-pattern is putting too much decision logic inside *ngIf. The directive is best when the expression is easy to read.
Prefer this:
Over this:
The second version works, but it pushes too much logic into the template. Move complex checks into a component property or computed getter so the UI stays understandable.
*ngIf else Versus Modern @if
In modern Angular, the new built-in control-flow syntax such as @if and @else is becoming the newer style. However, *ngIf else is still common in existing codebases and remains important to understand.
So if you see:
That is conceptually the same kind of conditional rendering. The syntax is newer, but the template idea is similar. If you are maintaining older Angular templates or learning from existing projects, *ngIf else is still worth knowing well.
A Full Example With Async Data
Here is a realistic component that shows a loading message until data arrives:
This is a straightforward pattern for asynchronous screens because it keeps loading UI and ready UI clearly separated.
Common Pitfalls
The most common pitfall is trying to put the fallback content directly inside the else clause instead of using an ng-template reference.
Another mistake is stuffing complex business logic into the *ngIf expression. The template becomes harder to debug and harder to read.
A third issue is forgetting that *ngIf adds or removes elements from the DOM rather than merely hiding them with CSS. That affects component lifecycle and view state.
Finally, some developers mix *ngIf else and newer @if syntax without team conventions. Pick a style that fits the codebase and use it consistently.
Summary
- '
*ngIf="condition; else templateRef"renders alternate content when the condition is false.' - The fallback must be an
ng-templateidentified by a template reference. - Use
thenandelsetogether when both branches are large. - Keep template conditions readable by moving complex logic into the component.
- Modern Angular also offers
@if, but*ngIf elseremains common and useful.

