Control Flow
Control flow of template blocks for conditionally showing, hiding and repeating elements.
@if
@if
Conditionally displays its content when its condition expression is truthy.
Referencing the conditional expression's result
The @if
conditional supports saving the result of the conditional expression into a variable for reuse inside of the block.
@switch
@switch
This block provides an alternate syntax to conditionally render data.
The value of the conditional expression is compared to the case expression using ===
.
Optionally use @default
block. Its contents only displays if none of the preceding case expressions match the value.
@for
@for
To render content of a block for each item in a collection.
The collection can be any javascript iterable
.
But there are performance advantages of using a regular Array
.
track
track
For tracking changes in the collection.
Using track
can significantly enhance your application's performance.
For collections that do not undergo modifications like (no items moved, added, or deleted), using
track $index
is enought.But for collections with mutable data or frequent changes, select a property that uniquely identifies each item to use as your track expression.
@empty
@empty
To define fallback content if the items
list is empty.
$index
and others
$index
and othersThere are several implicit and always available variables that Angular provides in @for
or ngFor
blocks.
$count
: Number of items in a collection iterated over.$index
: Index of the current row.$first
: Whether the current row is the firts row.$last
: Whether the current row is the last row.$even
: Whether the current index is even.$odd
: Whether the current index is odd.
Last updated