1import { Inject } from '@angular/core';
2import { DOCUMENT } from '@angular/common';
3
4export class MyClass {
5
6 constructor(@Inject(DOCUMENT) private document: Document) {
7 this.window = this.document.defaultView;
8 }
9
10 check() {
11 console.log(this.document);
12 console.log(this.window);
13 }
14
15}
1// in app.module.ts
2providers: [
3{ provide: Window, useValue: window }
4]
5
6// in component:
7Then you can just inject it directly anywhere you want - e.g. in a service:
8
9constructor(private window: Window) {
10// ...