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.

Value change in observable subscription not triggered in async pipe

See original GitHub issue

I’m submitting a…


[ ] Regression (a behavior that used to work and stopped working in a new release)
[x] Bug report  
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior

A change of a value (observable ‘A’) that is triggered by a subscribed observable (observable ‘B’) in a smart component does not reflect to the dumb component that is fed by an async pipe of observable ‘A’. We use a store to save all of our app’s states (see plunkr).

Expected behavior

The change can be seen in the dumb component as soon as one changes the value.

Minimal reproduction of the problem with instructions

http://plnkr.co/edit/l61VBFuybdCyyoNZIKro?p=preview

Enter some value (e.g. ‘test’) in the input field and click the button. This publishes the value in the observable ‘value’. In the app.ts one subscribes to that observable, so we get that value there. In the body of that subscribe the entered value is published in another observable ‘name’. The dumb component is fed by an async pipe which subscribes on the observable ‘name’. ‘test’ can not be seen in the dumb component now.

If you hit the button a second time, you can see the value in the dumb component. You can also delay the new value with a setTimeout to get it working.

What is the motivation / use case for changing the behavior?

Environment


Angular version: 5.2.0


Browser:
- [x] Chrome (desktop) version XX
- [ ] Chrome (Android) version XX
- [ ] Chrome (iOS) version XX
- [ ] Firefox version XX
- [ ] Safari (desktop) version XX
- [ ] Safari (iOS) version XX
- [ ] IE version XX
- [ ] Edge version XX
 
For Tooling issues:
- Node version: XX  
- Platform:  

Others:

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
trotylcommented, Jan 17, 2018

@pschmoek It’s the intrinsic evil of synchronous bi-directional event stream, not specific to Angular part. I won’t suggest depending on this kind of vulnerable code.

Angular’s view rendering happens after OnInit (consider AfterViewChecked), the order for combing OnInit and async pipe is expected.

1reaction
trotylcommented, Jan 17, 2018

You can see that your observable is emitting undefined value if you add a console.log, it’s the problem of your own data source:

name$ = this.store.state$.pipe(
  tap(n => console.log(`tap1: ${JSON.stringify(n)}`)),
  map(s => s.name),
  tap(n => console.log(`tap2: ${n}`)),
  distinctUntilChanged(),
  tap(n => console.log(`tap3: ${n}`)),
);
Read more comments on GitHub >

github_iconTop Results From Across the Web

Why is observable subscribe() not triggered while the async ...
user has changed, and passes the new value to the async pipe. Whereas the code in ngOnInit is executed once and only once,...
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 >
Angular Async Pipe - Here's how to use it like a boss
Every time a new value is emitted the async pipe will automatically mark your component to be checked for changes. And best of...
Read more >
Why You Should Not Put Any Logic in the RxJS Subscribe ...
The async pipe will trigger change detection when a new value is emitted, even with the OnPush strategy enabled. You would have to...
Read more >
Angular and RxJS. A deep dive into async pipe | by Erxk
When a value emits from the async pipe's source observable, it will mark the view to be checked for changes via markForCheck() ....
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