3

In my Angular 2 application I provide the window object using what is described here: Angular2 - How to inject window into an angular2 service.

However the ngc compiler used for AOT returns several errors. First I had to change the way I provide the dependency (note the 'Window'):

@NgModule({        
  providers: [
    { provide: 'Window',  useValue: window }
  ],
  ...
})
export class AppModule {}

And in my component (note the type 'any'):

@Component({ ... })
export default class MyComponent {
    constructor (
        @Inject('Window') private window: any
    ) {}
...

However I still get the following error thrown by the ngc compiler in my module ngfactory:

Property 'window' does not exist on type

Again everything is working fine with the tsc compiler.

Community
  • 1
  • 1
j3r6me
  • 798
  • 1
  • 5
  • 21

2 Answers2

2

In the end I fixed my issue by following exactly what is described here: http://juristr.com/blog/2016/09/ng2-get-window-ref/

j3r6me
  • 798
  • 1
  • 5
  • 21
1

The following, simple solution did the trick for me:

Under "@NgModule", in "providers" section:

{provide: 'window', useFactory: getWindow }

Make sure to export the "getWindow" method:

export function getWindow() { return window; }

Source - https://github.com/angular/angular/issues/14050

Yonatan Ayalon
  • 1,959
  • 18
  • 19