Change detection stops working when calling detectChanges outside of angular
See original GitHub issueI 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 setTimeout
s 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:
- Created 4 years ago
- Reactions:1
- Comments:7 (4 by maintainers)
@adamdport ,with your example, the reason
change detection
will not work, because you run thethis.changeDetectorRef.detectChanges();
in thengOnInit()
, and it will cause allevent listeners
such asblur
be added outsidengZone
, 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!@JiaLiPassion https://github.com/adamdport/cordovaZoneIssue