setTimeout(()=>{}, 0);
Causes Angular to run change detection for the whole application, like ApplicationRef.tick
zone.js patches async APIs (addEventHandler, setTimeout, …) and runs change detection after a callback was completed.
This is because the setTimeout method has been monkey patched to be intercepted by angular which upon interception triggers change detection. In other words everytime setTimeout is called a change detection happens.
每次setTimeout被调用时,都会触发Angular的change detection.
伪代码如下:
const temp = window.setTimeout;
window.setTimeout = (...args) => {
temp(...args)
angular.triggerChangeDetection(); // or w.e they use
}
So the 0 is to make the change detection happen immediately and the empty function is because we don’t want to do anything.