3

Is it possible to access angular(2+) ZoneJS instance (NgZone) without injecting it inside constructor like this:

...

 const zone = window['<angular zonejs path in window object>']
...

For now when I developing my angular library I need to do this:

class Test {
 constructor( zone: NgZone ) { initmyplugin(zone); }
}

But I don't wanna user to care about initialization of NgZone.

Dariusz Filipiak
  • 2,858
  • 5
  • 28
  • 39

1 Answers1

4

You can get hold of NgZone instance by using the following code:

ng.probe(getAllAngularRootElements()[0]).injector.get(ng.coreTokens.NgZone)

See also

yurzui
  • 205,937
  • 32
  • 433
  • 399
  • There is a problem with this command when I've got compiled my bundle with: `ng build --aot -prod --no-sourcemap` and `ng.probe(getAllAngularRootElements()[0])` returns **null**. Do you know how to access ngzone in this application mode ? – Dariusz Filipiak Dec 08 '17 at 01:34
  • Check this https://github.com/gdi2290/angular-starter/blob/master/src/environments/environment.ts#L24-L28 Maybe this can help you – yurzui Dec 08 '17 at 12:35