Content projection with ngIf and asyncPipe: Observables still subscribed to even though they are not in the DOM
See original GitHub issueBug Report
Affected Package
The issue is caused by package @angular/core
Is this a regression?
Not sure
Description
Using asyncPipe on an observable that is ngIf’d out of the DOM still seems to fire the observable when combined with content projection (ng-content
).
I expected the observable to only be subscribed to when the content in the expansion bar is expanded (and thus present in the DOM).
This is a simplified example, where in my application because of this we are seeing multiple network calls happening upfront where we only want them to happen upon expansion of the expansion bar.
Minimal Reproduction
https://stackblitz.com/edit/angular-observable-ngif-behaviour If you inspect the DOM you will note that the contents of the expansion bars are not present, but the console shows that the observables fired.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:6 (3 by maintainers)
Top Results From Across the Web
async pipe inside ngIf still gets value - Stack Overflow
Everytime the If is true, the element gets recreated.. so the async pipe re-subscribes to the data$ observable causing it to emit again....
Read more >AsyncPipe - Angular
The async pipe subscribes to an Observable or Promise and returns the latest value it has emitted. When a new value is emitted,...
Read more >Handling Observables with NgIf and the Async Pipe
Everytime we use the async pipe, we create a subscription. If you're going to subscribe directly to Observables that initiate data transfer, ...
Read more >How to Build Compound Components in Angular - DEV Community ...
When we need to have different versions and use cases and make it flexible ... Using the async pipe, we subscribe to countries$...
Read more >Every angular developer should be familiar with these tips
The async pipe allows us to subscribe to a Promise or Observable from the template side and it will return emitted values. When...
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
Content projection will be resolved at first before projecting it. For these kind of scenarios we can use ng-templates which we can have better control over ng-content. Please have a look at the below link,
https://stackblitz.com/edit/angular-observable-ngif-behaviour-rlhwzm?file=src%2Fapp%2Fapp.component.ts
I modified your code with ng-template for the use case.
This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.