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.

Change detection stops working when calling detectChanges outside of angular

See original GitHub issue

I discovered this while trying to create a reproduction for #1034. However, I’ve managed to reproduce it outside Cordova too:

https://stackblitz.com/edit/angular-material2-issue-pfytvj

If you call detectChanges from runOutsideAngular, at a specific time, change detection stops working properly:

this.ngZone.runOutsideAngular(() => {
  this.changeDetectorRef.detectChanges();
});

Doing this from ngOnInit seems to reproduce the issue, while calling it from a button click does not. I’ve also seen it happen when called from a NavigationEnd router event.

It appears that button clicks (even clicks that aren’t bound to a function) and setTimeouts allow a single pass of change detection to occur, but focus, keypress, etc do not trigger change detection appropriately.

This didn’t used to cause issues until I recently updated packages, so I suspect the change was released in the past couple months.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
JiaLiPassioncommented, Jul 19, 2019

@adamdport ,with your example, the reason change detection will not work, because you run the this.changeDetectorRef.detectChanges(); in the ngOnInit(), and it will cause all event listeners such as blur be added outside ngZone, so all those events will not trigger change detection automatically. I know you just make a demo, so could you post the code in your real case? thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

angular - Call ChangeDetectorRef.detectChanges from ...
They are triggered outside angular zone. Angular components subscribe to them and in event handlers (observers) call ChangeDetectorRef.
Read more >
Angular Change Detection - How Does It Really Work?
Change detection works by detecting common browser events like mouse clicks, HTTP requests, and other types of events, and deciding if the view ......
Read more >
The Last Guide For Angular Change Detection You'll Ever Need
Angular's Change Detection is a core mechanic of the framework but (at ... This mechanism of syncing the HTML with our data is...
Read more >
Resolving zone pollution - Angular
Run tasks outside NgZone link ... In such cases, you can instruct Angular to avoid calling change detection for tasks scheduled by a...
Read more >
Is Angular Better Off Without Change Detection? | by Lew C
Know why Angular should get rid of its change detection strategy ... whether to detect changes or not, there is a chance that...
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