Listening to Angular Window Resize Events with RxJs | Task

Ole Ersoy
Jan - 31  -  1 min

Scenario

Our Angular service needs to react to window resize events.

Approach

Note that we are using @ngneat/until-destroy to destroy the subscription when the component is destroyed, and RxJS debounceTime to delay the event 200 milliseconds after the window resize has completed.

fromEvent(window, 'resize')
.pipe(untilDestroyed(this), debounceTime(200))
.subscribe(() => console.log('Window resizing'));

Demo