问题
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