Combine multiple "| async as" in the same ngIf
See original GitHub issueWhich @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:
- Created 2 years ago
- Reactions:38
- Comments:7 (6 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@nicolaric Hello, there is another workaround you can use in the template, which you didn’t mention:
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:
This assumes you only want to alias, and not actually render conditionally.
You can also combine them directly in the template. It’s similar to the above, but already works now:
This assumes you only want to alias, and not actually render conditionally.