I am using Angular 2.
Is there a way to check whether the code now is in NgZone?
Something like this?
constructor(ngZone: NgZone) {}
foo()
{
console.log(this.ngZone.isInZoneNow);
}
NgZone notifies Angular when to perform the change detection process (e.g. a DOM event with bound listener is one of the triggerers). However, if in response to an event you directly manipulate the DOM or simply perform an action which does not require bindings update, the process is redundant.
Angular 2 runs inside of its own special zone called NgZone and this special zone extends the basic functionality of a zone to facilitate change detection. It is Running inside a zone allows to detect when asynchronous tasks.
Angular uses the zone to patch async APIs(addEventListener, setTimeout(), ...) and uses notifications from these patched APIs to run change detection every time some async event happened. Also zonejs is useful for debugging, testing, profiling. It helps you see whole call stack if you face with some error.
Oh, found NgZone has a static isInAngularZone
function.
So can use NgZone.isInAngularZone()
to know.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With