Quantcast
Channel: Angular window resize event - Stack Overflow
Viewing all articles
Browse latest Browse all 20

Answer by gsaandy for Angular window resize event

$
0
0

Another approach that I took was

import {Component, OnInit} from '@angular/core';import {fromEvent} from "rxjs";import {debounceTime, map, startWith} from "rxjs/operators";function windowSizeObserver(dTime = 300) {  return fromEvent(window, 'resize').pipe(    debounceTime(dTime),    map(event => {      const window = event.target as Window;      return {width: window.innerWidth, height: window.innerHeight}    }),    startWith({width: window.innerWidth, height: window.innerHeight})  );}@Component({  selector: 'app-root',  template: `<h2>Window Size</h2><div><span>Height: {{(windowSize$ | async)?.height}}</span><span>Width: {{(windowSize$ | async)?.width}}</span></div>  `})export class WindowSizeTestComponent {  windowSize$ = windowSizeObserver();}

here the windowSizeObserver can be reused in any component


Viewing all articles
Browse latest Browse all 20

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>