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.

3
  • Also I don't wanna use injector angular.io/api/core/Injector to keep library framework independent. Commented Dec 3, 2017 at 14:33
  • ng.coreTokens.NgZone Commented Dec 3, 2017 at 15:02
  • Sorry @yurzui, but this is not an instance... Commented Dec 3, 2017 at 21:41

1 Answer 1

4

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

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

See also

Sign up to request clarification or add additional context in comments.

2 Comments

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 ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.