Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check whether the code is in NgZone?

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);
  }
like image 880
Hongbo Miao Avatar asked Jul 04 '16 05:07

Hongbo Miao


People also ask

What is NgZone in Angular?

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.

What would be a good use for NgZone service?

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.

What is the purpose of zone dependency in Angular project?

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.


1 Answers

Oh, found NgZone has a static isInAngularZone function.

So can use NgZone.isInAngularZone() to know.

like image 98
Hongbo Miao Avatar answered Sep 30 '22 19:09

Hongbo Miao