Skip to content

ngIf improvements for Rx #13061

@mhevery

Description

@mhevery

Goals

When writing Rx style code it is often necessary to write code like this

{{ (userObs|async).?lastName}}, {{ (userObs|async).?firstName}}

The issue is that:

  1. every async creates a listener and
  2. the need to use the .? makes coding cumbersome.

What is needed is to be able to subscribe to the observable once and then refer to it in subsequent location. Additionally we need a way to display an alternative template while the observable has not yet yielded a value.

Proposal

  • Allow ngIf to assign data to local value.
  • Add an else feature to ngIf so that an alternative template can be shown until the observable yields a value.
<div *ngIf="userObs | async; else loading; let user">
  {{ user.lastName}}, {{ user.firstName }}
</div>
<template #loading>
  loading...
</template>

NOTE: the microsyntax is consistent with existing implementation of the microsyntax. For expanded form see:

<template [ngIf]="userObs | async" [ngIfElse]="loading" let-user>
  {{ user.lastName}}, {{ user.firstName }}
</template>
<template #loading>
  loading...
</template>

Metadata

Metadata

Assignees

Labels

area: commonIssues related to APIs in the @angular/common packagefeatureIssue that requests a new featurefreq3: high

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions