javascript / beginner
Snippet
Global Event Handling
The @HostListener decorator allows a component to respond to events from the global window or document objects.
snippet.js
1
2
3
4
@HostListener('window:resize', ['$event'])onResize(event: UIEvent) {console.log('Window width:', window.innerWidth);}
angular
Breakdown
1
@HostListener('window:resize', ['$event'])
Configures the method to trigger whenever the browser window is resized.
2
console.log('Window width:', window.innerWidth);
Accesses the global window object to read and log the current viewport width.