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.

Improve async pipe for single object observables

See original GitHub issue

I’m submitting a …

[x] feature request

Current behavior Main motivation: print multiple object properties by using the async pipe with 1 network request

For array observables myObjects$: MyObject[] you can use the async pipe in the following way:

<div *ngFor="let myObject of myObjects$ | async">
  <div>{{myObject.property1}}</div>
  <div>{{myObject.property2}}</div>
  <div>{{myObject.property3}}</div>
</div>

The code above will end in 1 network request.

For single object observables myObjects$: MyObject you can use the async pipe in the following way:

<div>
  <div>{{(myObjects$ | async)?.property1}}</div>
  <div>{{(myObjects$ | async)?.property2}}</div>
  <div>{{(myObjects$ | async)?.property3}}</div>
</div>

The code above will end in 3 network requests 😦

For sure you could cache the observable .publishReplay(1).refCount() (only 1 network request) but all together it looks odd.

Expected behavior Add support for a local template variable e.g.:

<div *ngIf="let myObject = myObjects$ | async">
  <div>{{myObject.property1}}</div>
  <div>{{myObject.property2}}</div>
  <div>{{myObject.property3}}</div>
</div>

What is the motivation / use case for changing the behavior? Beautiful code for printing multiple object properties by using the async pipe with 1 network request.

Please tell us about your environment: Linux

  • Angular version: 2.0.2
  • Browser: All
  • Language: TypeScript 2.0.3
  • Node (for AoT issues): 6.7.0

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:15
  • Comments:9 (7 by maintainers)

github_iconTop GitHub Comments

4reactions
DzmitryShylovichcommented, Oct 12, 2016

For sure you could cache the observable .publishReplay(1).refCount()

there’s a shortcut - share(). But I agree it would be very very useful.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to avoid multiple async pipes in Angular | by Yury Katkov
One alternative to this approach would be to use multiple async pipes and manually make sure that it uses shared observables. I find...
Read more >
Subscribing to properties of an object with async pipe [duplicate]
You can use the async pipe once, and assign its result to a template variable, then use it to access the properties you...
Read more >
Angular: Use Observable Subscriptions & Async-Pipe to ...
Async -Pipe is an Angular built in tool to manage Observable-Subscriptions. We can easily simplify functionality of an Angular code using the async...
Read more >
The Async Pipe - a Better way of passing Observable data to ...
no they don't but the thing is, http services do not emit unless requested... so subscribing to them, by nature, returns one value...
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 >

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