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.

AsyncPipe doesn't work with RxJs Subject

See original GitHub issue

I’m submitting a … (check one with “x”)

[X] bug report => search github for a similar issue or PR before submitting
[ ] feature request
[ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior AsyncPipe works properly with BehaviorSubject, but it don’t work with Rx Subject. More info: http://stackoverflow.com/questions/39902413/angular-2-0-1-asyncpipe-doesnt-work-with-rx-subject

Expected behavior Works with Subject

Minimal reproduction of the problem with instructions

export class AppComponent {
  foo = new Subject<Array<number>>();

  constructor(){
     setTimeout(() => {
          this.foo.next([1,2,3]);
     }, 3000);
  }

  getFoo(){
     return this.foo.asObservable();
  }
}

<span *ngFor="let e of foo.asObservable() | async">{{e}}</span>

What is the motivation / use case for changing the behavior? I use Subject because I somethines need subscribe to the service when data is loaded an make decisions. BehaviorSubject forces me to initialize the object with an empty data.

Please tell us about your environment: Not required

  • Angular version: 2.0.X Angular 2.0.1
  • Browser: All
  • Language: all
  • Node (for AoT issues):
    4.4.4

Issue Analytics

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

github_iconTop GitHub Comments

10reactions
wardbellcommented, Jun 24, 2017

Closing because we won’t document such a narrow use case in the general Angular docs. This would be better as a Stack Overflow Q&A

7reactions
robwormaldcommented, Oct 6, 2016

The problem here is calling a method from your template - this means every time change detection runs, you’re calling your getFoo() function, which returns a new instance of the observable, which resets the async pipe.

As @DzmitryShylovich’s example shows, its generally preferable to bind to properties on your component:

export class AppComponent {
  foo = new Subject<number[]>();
  foo$ = Observable<number[]>
  constructor(){
    this.foo$ = this.foo.asObservable();
     setTimeout(() => {
          this.foo.next([1,2,3]);
     }, 3000);
  }
}
<span *ngFor="let e of foo$ | async">{{e}}</span>
Read more comments on GitHub >

github_iconTop Results From Across the Web

Async pipe not working with Subject - Stack Overflow
The issue is that I dont see any of the two li , although the assumption is that the first one should appear...
Read more >
Async not working for Subject.asObservable - Ionic Angular
I've come across what appears to be an access issue. I have a Subject in a service, shared via a public accessor as...
Read more >
Angular Reactive Templates with ngIf and the Async Pipe
This approach works great, but one potential issue is that now we have to manually manage the subscriptions of this Observable. In the...
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 pipe....
Read more >
Async Pipe Is Broken in Angular - YouTube
Learn why async pipe is broken and how to fix this problem. In last versions of Angular default value of async pipe is...
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