question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Combine multiple "| async as" in the same ngIf

See original GitHub issue

Which @angular/* package(s) are relevant/releated to the feature request?

common

Description

Currently it is not possible to have the following:

<ng-container *ngIf="(data1$ | async as data1) && (data2$ | async as data2)">
  <div>{{ data1 }}</div>
  <div>{{ data2 }}</div>
</ng-container>

Workarounds are as follow:

<ng-container *ngIf="data1$ | async as data1">
  <ng-container *ngIf="data2$ | async as data2">
    <div>{{ data1 }}</div>
    <div>{{ data2 }}</div>
  <ng-container>
</ng-container>

Or even something like this:

// TS
data$ = combineLatest([
  data1$,
  data2$
]).pipe(
  map(([data1, data2]) => ({
    data1,
    data2
  }))
)

<!-- HTML -->
<ng-container *ngIf="data$ | async as data">
  <div>{{ data.data1 }}</div>
  <div>{{ data.data2 }}</div>
</ng-container>

This, in my eyes, is not super clean and is more difficult to read. It would be very helpful to extend the as keyword for this.

Proposed solution

As described above, it would be super clean to have something like this:

<ng-container *ngIf="(data1$ | async as data1) && (data2$ | async as data2)">
  <div>{{ data1 }}</div>
  <div>{{ data2 }}</div>
</ng-container>

Alternatives considered

I don’t see any alternatives, except of using the workarounds which I use today already.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:38
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

6reactions
lukasmattacommented, Oct 22, 2021

@nicolaric Hello, there is another workaround you can use in the template, which you didn’t mention:

<ng-container *ngIf="{data1: data1$ | async, data2: data2$ | async} as d">
  <ng-container *ngIf="d.data1 && d.data2">
    {{d.data1}} {{d.data2}}
  </ng-container>
</ng-container>
6reactions
Airbladercommented, Oct 22, 2021

the as syntax is specfic to the ngIf directive

I don’t think that’s correct; it can also be used with any other custom structural directive. Of course your point that it’s tied to the directive and not the pipe remains valid.

If the team wanted to support the use case, but not the solution, then a syntax like this would be an alternative by allowing a kind of destructuring:

*ngIf="[a$ | async, b$ | async] as [a, b]"

This assumes you only want to alias, and not actually render conditionally.


Or even something like this:

You can also combine them directly in the template. It’s similar to the above, but already works now:

<... *ngIf="{ a: a$ | async, b: b$ | async } as v">
    {{ v.a }}
    {{ v.b }}

This assumes you only want to alias, and not actually render conditionally.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ngIf with multiple async pipe variables - angular - Stack Overflow
Subscribe inside the component and write to variables; To combine the two observables inside the component with say withLatestFrom; Add null ...
Read more >
How to avoid multiple async pipes in Angular | by Yury Katkov
TL;DR: make sure that you don't expand the same observable more than once in your async pipes. Use ngIf trick for observable expansion...
Read more >
How to simplify multiple async pipes - DEV Community ‍ ‍
We have a single observable to manage both subscriptions. Use combineLatest to merge and combine the data and use the template.
Read more >
combine or multiple async pipes? : r/Angular2 - Reddit
Multiple observables in a template - combine or multiple async pipes? ... *ngIf="dogs$ | async"> (you might need this observable again later ...
Read more >
Angular – *ngIf with multiple async pipe variables - iTecNote
Subscribe inside the component and write to variables · To combine the two observables inside the component with say withLatestFrom · Add null...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found