Sub topic 4
Keeping an expression result with as
This chapter assumes you know Angular components and templates, interpolation, and property and event binding. Some familiarity with signals or observables helps. By the end you will be able to capture the result of an @if expression in a named variable and use it inside the block.
Why you need an alias
An @if condition is often an expression you also want to use in the block body. Two typical cases are the value emitted by an observable through the async pipe, and the result of a call or a deep property lookup.
Without an alias you must evaluate the expression twice, once for the condition and once for the content. With the async pipe this is a real problem, because each use of the pipe creates its own subscription. Two uses mean two subscriptions, and possibly two HTTP requests.
The as clause solves this. You write the expression once, add a semicolon, and name the result.
@if (expression; as name) {
<p>{{ name }}</p>
}The block renders only when the expression is truthy. Inside the block, name holds the evaluated value.
Aliasing an async pipe result
The most common use of as is with async. The component below exposes an observable. The template subscribes once and keeps the emitted user in user.
import { Component } from '@angular/core';
import { AsyncPipe } from '@angular/common';
import { Observable, of } from 'rxjs';
interface User {
name: string;
}
@Component({
selector: 'app-profile',
imports: [AsyncPipe],
template: `
@if (user$ | async; as user) {
<h2>{{ user.name }}</h2>
} @else {
<p>Loading profile...</p>
}
`,
})
export class ProfileComponent {
user$: Observable<User> = of({ name: 'Ada' });
}The async pipe returns null until the observable emits. A null result is falsy, so the @else branch shows "Loading profile...". After the emission, the value is truthy. The @if branch renders, and user is a User object.
The async pipe must be in the component's imports array. Control flow blocks need no import, but pipes do.
Where the alias is visible
The alias exists only inside the @if block that declares it. It is not available in the @else branch or elsewhere in the template.
@if (user$ | async; as user) {
<p>{{ user.name }}</p> <!-- works -->
} @else {
<p>{{ user }}</p> <!-- error: user is not defined here -->
}The alias also has a type. The async pipe returns User | null. Because the block runs only when the value is truthy, Angular narrows user to User inside it. You can read user.name without a null check. A later chapter covers type narrowing in more detail.
Aliases with @else if
The as clause also works on an @else if branch. Each branch has its own alias with its own scope.
@if (primary$ | async; as primary) {
<p>Primary: {{ primary.name }}</p>
} @else if (backup$ | async; as backup) {
<p>Backup: {{ backup.name }}</p>
} @else {
<p>No contact available.</p>
}Here primary is visible only in the first branch and backup only in the second. Both observables are subscribed to when the template is first checked, even if the first one is truthy. The @else if expression is evaluated only when earlier conditions fail, but once evaluated it keeps its subscription.
Common mistakes
- Using a falsy value that you want to display. If the expression can legitimately be
0,'', orfalse, the block will not render. An alias does not change the truthiness test. For a numeric count that may be0, compare explicitly, for example@if (count$ | async; as count)hides a count of0. Use@if ((count$ | async) !== null)and read the value another way. - Using the alias outside the block. The name is scoped to the block, as shown above.
- Forgetting the semicolon. The alias goes after a semicolon, not a comma:
; as user. - Forgetting to import
AsyncPipe. Without it the template fails to compile.
How it compares with *ngIf
The older directive used the same idea: *ngIf="user$ | async as user". The built-in block separates the condition from the alias with a semicolon. The else branch is written inline with @else, so you no longer need an ng-template reference.
Exercise
A component has count$: Observable<number> that emits 0 as its first value. The template is @if (count$ | async; as count) { <p>Count: {{ count }}</p> } @else { <p>Loading...</p> }. What does the user see when the observable emits 0, and why?
Show answer
The user sees "Loading...". The as clause does not change the condition, and 0 is falsy, so the @else branch renders. To show the zero, compare against null explicitly, for example @if ((count$ | async) !== null) { ... }, and read the value in the body with a second async use or by storing it as a signal with toSignal.
Exercise
In which parts of an @if (...; as name) { ... } @else { ... } block can you use name?
Show answer
Only inside the @if body. It is not available in the @else branch or outside the block.