Use zone.js to detect current execution context from anywhere?

你。 提交于 2019-12-08 04:03:53

问题


With zone.js is it possible to determine the current execution context from anywhere? Ie., if a zone-bound function calls another function which calls setTimeout(myFn) can I determine the current execution context from within myFn()? If so, please provide a simple example of how to do so.


回答1:


Every time you fork() a zone, accessing the zone object within that zone's context will always return the forked zone.

The following experiment demonstrates this behavior:

var b = function() { console.log('-->',zone) }; 
var a = function() { setTimeout(b,5); }; 
zone.fork().run(function() { zone.x = 'hi'; a(); }); 
setTimeout(function() { console.log('==>', zone); }, 1);
setTimeout(function() { console.log('==>', zone); }, 10);

Here's what happens when you paste it into the console:



来源:https://stackoverflow.com/questions/27164756/use-zone-js-to-detect-current-execution-context-from-anywhere

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!