src/helpers/window.helper.ts
A utility service for working with the window object in Angular applications.
This service provides methods to retrieve the window object, the href location object, and touch capabilities.
Example :Inject the GetWindow service and use its methods:
constructor(private getWindow: GetWindow) { }
Or instance a new class const getWindow = new GetWindow()
const window = this.getWindow.window(); const href = this.getWindow.href(); const touch = this.getWindow.touch();]
Methods |
constructor(document: Document)
|
||||||||
Defined in src/helpers/window.helper.ts:24
|
||||||||
Initializes a new instance of the
Parameters :
|
href |
href()
|
Defined in src/helpers/window.helper.ts:44
|
Allow to get the href location object inside ssr
Returns :
string | URL
|
touch |
touch()
|
Defined in src/helpers/window.helper.ts:56
|
Allow to get the origin location object inside ssr
Returns :
boolean
|
window |
window()
|
Defined in src/helpers/window.helper.ts:36
|
Allow to get the window object inside ssr
Returns :
| null
|
import { DOCUMENT } from '@angular/common'
import { Injectable, Inject } from '@angular/core'
/**
* A utility service for working with the window object in Angular applications.
*
* This service provides methods to retrieve the window object, the href location object, and touch capabilities.
*
* @example
* Inject the GetWindow service and use its methods:
* constructor(private getWindow: GetWindow) { }
*
* Or instance a new class
* const getWindow = new GetWindow()
*
* const window = this.getWindow.window();
* const href = this.getWindow.href();
* const touch = this.getWindow.touch();]
* @returns {void}
*/
@Injectable({
providedIn: 'root',
})
export class GetWindow {
/**
* Initializes a new instance of the `GetWindow` class.
* @constructor
* @param {Document} document - The injected DOCUMENT to get a reference to the window object in a way that's safe for SSR.
*/
constructor(@Inject(DOCUMENT) private document: Document) { }
/**
* Allow to get the window object inside ssr
* @returns {Window | null} - The window object.
*/
window(): Window & typeof globalThis | null {
return this.document.defaultView
}
/**
* Allow to get the href location object inside ssr
* @returns {string | URL} - The href location object.
*/
href(): string | URL {
const window = this.window()
if (window) {
return window.location.href
}
return ''
}
/**
* Allow to get the origin location object inside ssr
* @returns {boolean} - `true` if the browser supports touch events, otherwise `false`.
*/
touch(): boolean {
const window = this.window()
if (window) {
return 'ontouchstart' in window || navigator.maxTouchPoints > 0
}
return false
}
}