Last active
January 9, 2018 15:24
-
-
Save gmocamilotd/9acbb60cc8e144de54c251605dc6ec24 to your computer and use it in GitHub Desktop.
angular access window object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
import { WindowRef } from './WindowRef'; | |
@Component({...}) | |
class MyComponent { | |
constructor(private winRef: WindowRef) { | |
// getting the native window obj | |
console.log('Native window obj', winRef.nativeWindow); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { WindowRef } from './WindowRef'; | |
... | |
@NgModule({ | |
... | |
providers: [ WindowRef ] | |
}) | |
export class AppModule{} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
https://plnkr.co/edit/9qmBCVrmBZj3mPQjM0Zc?p=preview | |
fuente: | |
https://juristr.com/blog/2016/09/ng2-get-window-ref/ | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Wrapping window | |
A very straightforward and easy way to wrap window is by creating an Angular service. That’s as easy as creating an ES6 class and decorating it with @Injectable. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Remember the $window object in Angular 1? Turned out to be quite useful from now and then. But what about Angular? $window doesn't exist there. What's the alternative? How can I inject the Window object into my Angular components? | |
Especially Angular isn’t only designed to run within your browser, but also on mobiles, the server or web workers where objects like window may not be available. | |
Therefore the suggested approach is to wrap such objects and inject them through the dependency injection mechanism. This way it is possible to change the concrete runtime instance of a given object based on the environment the Angular application is running. The result we wanna achieve is the following: | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from '@angular/core'; | |
function _window() : any { | |
// return the global native browser window object | |
return window; | |
} | |
@Injectable() | |
export class WindowRef { | |
get nativeWindow() : any { | |
return _window(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment