Core
Window
The Window provider offers a safe and consistent way to access the global window object across different platforms.
This provider abstracts direct references to window, ensuring compatibility with server-side rendering (SSR) and other environments where window may not be available. It works by injecting and accessing the defaultView property of the token from Angular's core package. Therefore, it is limited to Angular's implementation of the DOM in non-browser environments.
Provider Setup
To use window utilities, add the provideWindow function to your application configuration:
import { provideWindow } from '@/app/core/window';
export const appConfig: ApplicationConfig = {
providers: [
provideWindow(),
// ...other providers
],
};Usage
To access the global window object, you can inject the WINDOW injection token into your services or components:
protected window = inject(WINDOW);