Skip to content

Template Local Variables #15280

Closed
Closed
@kylecordes

Description

@kylecordes

[X] feature request

Current behavior

The new syntax is a great improvement!

<div *ngIf="userStream|async as user">...</div>

... And was discussed at length in #15020.

Expected behavior

The ability to capture the async pipe output and make it available as a template variable, is much appreciated and very useful. Unfortunately, this capability is currently bound closely to *ngIf; it is only available is a feature of *ngIf. (I am aware that there is something similar for *ngFor, unrelated to this feature request).

Here's what I request: that this capability be yanked apart from the conditional (*ngIf), and made available elsewhere.

I don't know what syntax would make the most sense. Perhaps:

<div *ngLet="userStream|async as user">...</div>

Or alternatively; it would be excellent to have a syntax to directly use any observable anywhere in a template, with the reactivity handled by Angular, and without worrying about potentially multiple async pipe instances needlessly subscribing many times. For example, imagine the following syntax:

<p>Hello, {{ userStream^.name }}</p>

What is the motivation / use case for changing the behavior?

I have a use case in front of me, the details which don't matter, where:

  • I have a handful of observable streams with data to use in the template.
  • Some of them occasionally have falsely values; but I still want to use them even if that is the case.
  • Currently I'm doing some hackery to deal with the falsely problem, and nesting a big chunk of my template in a handful of nested *ngIfs which exists solely for the purpose of giving access to this ability to capture the async values to template variables.

Metadata

Metadata

Assignees

Labels

area: coreIssues related to the framework runtimefeatureIssue that requests a new featurefeature: under considerationFeature request for which voting has completed and the request is now under considerationfreq3: highhotlist: google

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions