Conditional Rendering
Learn about the various methods for conditionally rendering content in Aurelia 2, with detailed explanations and examples.
Conditional rendering allows you to dynamically show or hide parts of your view based on your application's state. Aurelia 2 provides three primary directives for conditional rendering, each suited for different scenarios.
Quick Reference
if.bind
Simple true/false conditions
Adds/removes elements
Best for infrequent changes
show.bind
Toggle visibility
Hides/shows elements
Best for frequent changes
switch.bind
Multiple conditions
Adds/removes elements
Best for enum-like values
Using if.bind
The if.bind directive conditionally adds or removes elements from the DOM based on a boolean expression. When the expression is false, Aurelia completely removes the element and its descendants, cleaning up resources, events, and custom elements.
Basic Usage
<div if.bind="isLoading">Loading...</div>
<div if.bind="user.isAuthenticated">Welcome back, ${user.name}!</div>If/Else Structures
Use else immediately after an if.bind element to create branching logic:
<div if.bind="user.isAuthenticated">
Welcome back, ${user.name}!
</div>
<div else>
Please log in to continue.
</div>Else-If Chains
You can chain additional conditions by combining else and if.bind on the same element:
This also works on direct custom elements, not just native elements or explicit <template> nodes:
Ordering Rules for else if
else if is still an else chain. The branch rules are:
elsemust belong to the immediately preceding conditional branch.elsemust appear beforeif.bindon the same element.Plain attributes and non-template-controller custom attributes may appear between
elseandif.bindon that same element.Another template controller between
elseandif.bindmeans the attributes do not form anelse ifbranch.A plain
elsebranch ends the chain. Anotherelseafter that is invalid.
Valid examples:
A controller-bearing structure between sibling branches breaks the chain:
This same-element combination is unsupported as else if because repeat separates the two template controllers:
Async Branch Transitions
If a leaving branch has async deactivation or an entering branch has async activation, Aurelia waits for those lifecycle promises as part of the branch swap.
In practice:
the current branch begins leaving first
the replacement branch starts only after the previous branch has finished its async leave work
a branch superseded during async attachment begins leaving immediately, with later branch work queued behind its teardown
once the replacement branch starts attaching, its DOM may already be present before the async
attaching()promise resolves
If you assert intermediate DOM state in tests, account for those lifecycle phases rather than assuming the entire swap happens in a single microtask.
Caching Behavior
By default, if.bind caches views and view models for performance. Disable caching when you need fresh instances:
With caching disabled, Aurelia deactivates and disposes a branch when a conditional change removes it. Returning to that branch creates a new view and view model.
When to Use: Use if.bind when elements change infrequently and you want to completely remove them from the DOM to save memory and improve performance.
Using show.bind
The show.bind directive toggles element visibility without removing them from the DOM. This is equivalent to setting display: none in CSS.
Basic Usage
hide.bind (inverse of show.bind)
hide is an alias of show with inverted logic:
This is equivalent to:
When to Use show.bind vs if.bind
Using switch.bind
The switch.bind directive handles multiple conditions elegantly, similar to a JavaScript switch statement. It's ideal for enum values or when you have several mutually exclusive conditions.
Basic Usage
Grouping Cases
Handle multiple values with a single case:
Fall-Through Behavior
Enable fall-through to show multiple cases:
Advanced Techniques
Dynamic Switch Expressions
Use computed expressions with switch.bind:
Conditional Slot Projection
Combine switch.bind with slots for dynamic content projection:
Nested Switches
Handle complex conditional logic with nested switches:
Performance Guidelines
Choosing the Right Directive
Frequent toggles: Use
show.bindto avoid DOM manipulation overheadInfrequent changes: Use
if.bindto remove elements and save memoryMultiple conditions: Use
switch.bindfor cleaner, more maintainable code
Optimization Tips
Important Restrictions
Case Usage Rules
The case attribute must be a direct child of switch.bind:
Default Case Placement
Place default-case as the last option for best practices:
Last updated
Was this helpful?