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.

Content projection with ngIf and asyncPipe: Observables still subscribed to even though they are not in the DOM

See original GitHub issue

Bug 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:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
ramanathan23commented, May 8, 2021

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.

image

0reactions
angular-automatic-lock-bot[bot]commented, Jun 10, 2021

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.

Read more comments on GitHub >

github_iconTop 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 >

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