How to get a Reference to the Window Object in an Angular 8 Application
~1 min read

Tags

  • Angular
  • Window Object

There are many articles on the web showing various methods of getting a reference to the window object in Angular (primarily through the dependency injection mechanism). However those that are popular on Google search are from 2016, 2017, etc, and the methods are mostly overly complicated (understandably).

If you’ve had issues with these like me, there is a much easier way to do this.

Just provide the Window object directly as a service within your core/app module. Like this:

providers: [
    { provide: Window, useValue: window }
]

Then you can just inject it directly anywhere you want - e.g. in a service:

constructor(private window: Window) {
    // ...
}

Credits: I found this little gem here.