Skip to content

Instantly share code, notes, and snippets.

@qdouble
Created September 8, 2016 12:10
Show Gist options
  • Save qdouble/2641d5013208b5102ce3375ff27a1d12 to your computer and use it in GitHub Desktop.
Save qdouble/2641d5013208b5102ce3375ff27a1d12 to your computer and use it in GitHub Desktop.
Window service
import { OpaqueToken } from '@angular/core'
var win = typeof window !== 'undefined' && window || <any>{};
export { win as window };
function CONST_EXPR(expr) {
return expr;
}
function _window(): any {
return window
}
export const WINDOW: OpaqueToken = CONST_EXPR(new OpaqueToken('WindowToken'));
export abstract class WindowRef {
get nativeWindow(): any {
throw new Error('unimplemented');
}
}
export class WindowRef_ extends WindowRef {
constructor() {
super();
}
get nativeWindow(): any {
return _window();
}
}
export const WINDOW_PROVIDERS = [
{ provide: WindowRef, useClass: WindowRef_ },
{ provide: WINDOW, useFactory: _window, deps: []}
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment