AsyncPipe doesn't work with RxJs Subject
See original GitHub issueI’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:
- Created 7 years ago
- Reactions:6
- Comments:16 (9 by maintainers)
Top 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 >
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 Free
Top 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
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
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: