Angular window resize event
Asked Answered
L

19

371

I would like to perform some tasks based on the window re-size event (on load and dynamically).

Currently I have my DOM as follows:

<div id="Harbour">
    <div id="Port" (window:resize)="onResize($event)" >
        <router-outlet></router-outlet>
    </div>
</div>

The event correctly fires

export class AppComponent {
    onResize(event) {
        console.log(event);
    }
}

How do I retrieve the Width and Height from this event object?

Thanks.

Lancers answered 20/2, 2016 at 18:42 Comment(3)
Not really an Angular question. look at the window object. You can get it's innerHeight and innerWidth properties..Coles
@Coles is correct, you just need to do console.log(event.target.innerWidth )Cottager
Thanks for the info Sasxa/Pankaj - I wasn't sure whether it was just a plain javascript thing or a Typescript thing or an Angular event thing. I'm climbing a very steep learning curve for myself here and appreciate your input.Lancers
P
766
<div (window:resize)="onResize($event)"
onResize(event) {
  event.target.innerWidth;
}

or using the HostListener decorator:

@HostListener('window:resize', ['$event'])
onResize(event) {
  event.target.innerWidth;
}

Or another way using HostListener decorator

@HostListener('window:resize', ['$event.target.innerWidth'])
onResize(width: number) {
   console.log(width);
}

Supported global targets are window, document, and body.

Until https://github.com/angular/angular/issues/13248 is implemented in Angular it is better for performance to subscribe to DOM events imperatively and use RXJS to reduce the amount of events as shown in some of the other answers.

Pish answered 20/2, 2016 at 19:17 Comment(24)
Is there any documentation about the syntax you use: window:resize ?Cloudless
@Cloudless haven't found docs. But it was repeatedly mentioned and is officially supported. Docs are not perfect yet.Corneliuscornell
Okay, so this is a kind of syntaxic sugar build into angular2 ?Cloudless
Exactly. You can use document, window, and body github.com/angular/angular/blob/…Corneliuscornell
Seems you're doing something wrong ;-) I'd suggest you create a new question with the code that allows to reproduce the problem.Corneliuscornell
inside the onResize(event) let screenHeight = Math.max(document.documentElement.clientHeight, window.innerHeight || 0); It's workingOrellana
perfect answer.. i do believe @HostListener is the cleaner way :) but make sure to import the HostListener first using import { Component, OnInit, HostListener } from '@angular/core';Ineligible
Quick tip: If want to trigger on first load too, implement ngAfterViewInit from @angular/core. angular.io/api/core/AfterViewInitAntiicer
Using Angular 6, @HostListener('window:resize', ['$event']) works for me, but only with window. Replacing window by document or body, the event is not triggered at all.Transatlantic
Angular only providers what the browser supports. If document of body don't emit that event, youcan't getit from Angular. Consult the browser docs which events are amitted by which object.Corneliuscornell
This is NOT THE BEST SOLUTION, hostlistener fires on every pixel, and multiple times, plus, if this is implemented in multiple components, will cause a lack or delays to show or change the elementsCourses
in fact, this will slow the app!Courses
You're still right :D I added a link to an issue where plans for improvement are discussed.Corneliuscornell
what about using debounceTime?Courses
Is a good idea but works better with the other answers with imperative event subscription.Corneliuscornell
Sure, however, for those who want to know how works HostListener with debounceTime, follow the link below plnkr.co/edit/3J0dcDaLTJBxkzo8Akyg?p=previewCourses
@Courses Nice one!Corneliuscornell
I'm using @HostListener('window:resize') @throttle(300), but do I need to unsubscribe to the window:resize event and if so, how?Ease
@Ease no. As a rule of thumb, if you subscribe imperatively (calling foo.subscribe(evt => ...), you should explicitly unsubscribe, if you declarativly subscribe, Angular will take care automatically.Corneliuscornell
@GünterZöchbauer Thanks for your reply. Do you have any reference for this?Ease
Hi, consider my answer, https://mcmap.net/q/92233/-angular-window-resize-event debounceTime won't reduce change detection cycles.Spreader
The only thing i couldn't figure out right away was that the onResize method has to be defined right after the @HostListener declaration in a kind of a callback way. Had not used this API before.Sioux
@Sioux @HostListene() is an annotation to onResize similar to @Input, @Output(), @ViewChild() @Inject(), ... and annotations always apply to the following element.Corneliuscornell
The global targets has been moved, currently you can find them here: github.com/angular/angular/blob/master/packages/compiler/src/…Spreader
C
132

I know this was asked a long time ago, but there is a better way to do this now! I'm not sure if anyone will see this answer though. Obviously your imports:

import { fromEvent, Observable, Subscription } from "rxjs";

Then in your component:

resizeObservable$: Observable<Event>
resizeSubscription$: Subscription

ngOnInit() {
    this.resizeObservable$ = fromEvent(window, 'resize')
    this.resizeSubscription$ = this.resizeObservable$.subscribe( evt => {
      console.log('event: ', evt)
    })
}

Then be sure to unsubscribe on destroy!

ngOnDestroy() {
    this.resizeSubscription$.unsubscribe()
}
Cotter answered 22/8, 2018 at 14:33 Comment(6)
The only way that has worked for me. Thanks!! :-) ... I just had to adjust your import. Perhaps my rxjs is newer: import { fromEvent, Observable,Subscription } from "rxjs";Eggbeater
Where can I add debounce(1000) in this?Fathom
Sorry for late reply: to add debounce, you'll want to use this.resizeSubscription$ = this.resizeObservable$.pipe(debounceTime(1000)).subscribe( evt => { console.log('event: ', evt) })Cotter
for debounce, import { debounceTime } from 'rxjs/operators';Salters
In spite of using debounceTime and running your subscribe callback less, Angular will run change detection on every event, as it's triggered inside the zone. You should check my answer that eliminates this problem.Spreader
Andddd if you don't want it to accumulate subscriptions each time the window resizes, do a this.resizeSubscription?.unsubscribe() just before you subscribe in ngOnInit!Flotow
A
81

@Günter's answer is correct. I just wanted to propose yet another method.

You could also add the host-binding inside the @Component()-decorator. You can put the event and desired function call in the host-metadata-property like so:

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
  host: {
    '(window:resize)': 'onResize($event)'
  }
})
export class AppComponent{
   onResize(event){
     event.target.innerWidth; // window width
   }
}
Audra answered 1/12, 2016 at 11:48 Comment(8)
I've tried every method on this page and it seems none of them work. Is there any official documentation about this.Alcoholism
how is the viewport height obtained on load?Briggs
@GiridharKarnik, have you tried doing a window.innerWidth inside ngOnInit, or ngAfterViewInit methods?Audra
@Alcoholism The API-reference can be found here Many of the references are autogenerated (I presume), and have a lack of examples or detailed explanation. Some api-references have a lot of examples. I could not find a concrete example from the docs about this though. Maybe it is hidden somewhere :PAudra
@John, if I do that, won't it be an on shot thing? Will ngOnInit or ngAfterViewInit fire each time I resize?Briggs
@GiridharKarnik Yes, you are correct. They will only fire once (each time the component is created). If you want to check every time you resize, use the @HostListener.Audra
here is a reference of how its usedBohunk
doesn't work for server-side or in angular universalCourses
S
52

There's a ViewportRuler service in angular CDK. It runs outside of the zone, supports orientationchange and resize. It works with server side rendering too.

@Component({
  selector: 'my-app',
  template: `
    <p>Viewport size: {{ width }} x {{ height }}</p>
  `
})
export class AppComponent implements OnDestroy {
  width: number;
  height: number;
  private readonly viewportChange = this.viewportRuler
    .change(200)
    .subscribe(() => this.ngZone.run(() => this.setSize()));
  constructor(
    private readonly viewportRuler: ViewportRuler,
    private readonly ngZone: NgZone
  ) {
    // Change happens well, on change. The first load is not a change, so we init the values here. (You can use `startWith` operator too.)
    this.setSize();
  }
  // Never forget to unsubscribe!
  ngOnDestroy() {
    this.viewportChange.unsubscribe();
  }

  private setSize() {
    const { width, height } = this.viewportRuler.getViewportSize();
    this.width = width;
    this.height = height;
  }
}

Stackblitz example for ViewportRuler

The benefit is, that it limits change detection cycles (it will trigger only when you run the callback in the zone), while (window:resize) will trigger change detection every time it gets called.

Spreader answered 21/7, 2019 at 9:8 Comment(7)
this should be the accepted answer. Has everything needed + this is angular CDK inbuilt functionality.Purgation
this answer needs to be upvoted more. In terms of performance and compatibility, this is the best solutionTyndareus
Tried this and got it working....the performance was drastically slower than options above.Fistula
@Fistula I've added an example to show how high performant this is.Spreader
This requires Angular Material componentsAphaeresis
No, it requires npmjs.com/package/@angular/cdk, I start my answer with that.Spreader
@Aphaeresis add cdk to your project: ng add @angular/cdkInpatient
L
44

The correct way to do this is to utilize the EventManager class to bind the event. This allows your code to work in alternative platforms, for example server side rendering with Angular Universal.

import { EventManager } from '@angular/platform-browser';
import { Observable } from 'rxjs/Observable';
import { Subject } from 'rxjs/Subject';
import { Injectable } from '@angular/core';

@Injectable()
export class ResizeService {

  get onResize$(): Observable<Window> {
    return this.resizeSubject.asObservable();
  }

  private resizeSubject: Subject<Window>;

  constructor(private eventManager: EventManager) {
    this.resizeSubject = new Subject();
    this.eventManager.addGlobalEventListener('window', 'resize', this.onResize.bind(this));
  }

  private onResize(event: UIEvent) {
    this.resizeSubject.next(<Window>event.target);
  }
}

Usage in a component is as simple as adding this service as a provider to your app.module and then importing it in the constructor of a component.

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'my-component',
  template: ``,
  styles: [``]
})
export class MyComponent implements OnInit {

  private resizeSubscription: Subscription;

  constructor(private resizeService: ResizeService) { }

  ngOnInit() {
    this.resizeSubscription = this.resizeService.onResize$
      .subscribe(size => console.log(size));
  }

  ngOnDestroy() {
    if (this.resizeSubscription) {
      this.resizeSubscription.unsubscribe();
    }
  }
}
Lucilius answered 7/5, 2017 at 16:22 Comment(7)
This doesn't ever fire on mobile as far as I can tell. How do you get the initial window size with this approach?Upstroke
Mobile does not have a window object, just as a server doesn't have a window. Im not familiar with the different mobile configurations but you should be able to easily adapt the above code to bind to the correct global event listenerLucilius
@Lucilius I'm a noob but this seems like the right answer. Unfortunatly I'm having no luck making my subscription Log in the component. Could you add to the answer how to subscribe to this in a component so that one can see the updates?Hawn
@Lucilius I'll make a plunker but this seemed to not work . The filter in the service seems weird tho https://mcmap.net/q/93424/-how-to-use-eventmanager-to-listen-to-window-resize-events-in-angular/1191635Hawn
I took the comment as he was using Ionic or NativeScript.Lucilius
Sorry I acknowledge that comment may be incorrect and misleading. I don't know about mobileLucilius
Note: addGlobalEventListener is @deprecated as of Angular 12 — No longer being used in Ivy code. To be removed in version 14.Westleigh
B
34

Here is a better way to do it. Based on Birowsky's answer.

Step 1: Create an angular service with RxJS Observables.

import { Injectable } from '@angular/core';
import { Observable, BehaviorSubject } from 'rxjs';

@Injectable()
export class WindowService {
    height$: Observable<number>;
    //create more Observables as and when needed for various properties
    hello: string = "Hello";
    constructor() {
        let windowSize$ = new BehaviorSubject(getWindowSize());

        this.height$ = (windowSize$.pluck('height') as Observable<number>).distinctUntilChanged();

        Observable.fromEvent(window, 'resize')
            .map(getWindowSize)
            .subscribe(windowSize$);
    }

}

function getWindowSize() {
    return {
        height: window.innerHeight
        //you can sense other parameters here
    };
};

Step 2: Inject the above service and subscribe to any of the Observables created within the service wherever you would like to receive the window resize event.

import { Component } from '@angular/core';
//import service
import { WindowService } from '../Services/window.service';

@Component({
    selector: 'pm-app',
    templateUrl: './componentTemplates/app.component.html',
    providers: [WindowService]
})
export class AppComponent { 

    constructor(private windowService: WindowService) {

        //subscribe to the window resize event
        windowService.height$.subscribe((value:any) => {
            //Do whatever you want with the value.
            //You can also subscribe to other observables of the service
        });
    }

}

A sound understanding of Reactive Programming will always help in overcoming difficult problems. Hope this helps someone.

Briggs answered 7/1, 2017 at 5:58 Comment(7)
I believe this is a error: this.height$ = (windowSize$.pluck('height') as Observable<number>).distinctUntilChanged();Observable<number>).distinctUntilChanged(); looks like you pasted in the distinctUntilChanged() twice in a rowKentish
I did not get you, eloborate please.Briggs
Change detection won't be fired as you're doing this event outside of Angular, believe that's what he meant.Kashakashden
I had an error saying 'pluck' does not exist on type BehaviorSubject. Changing the code to this.height$ = windowSize$.map(x => x.height) worked for me.Griz
@GiridharKamik Can you provide a solution that would subscribe on width & height at same time, which we could subscribe to both the width, height in 1 simple subscribeGowrie
@MarkPieszak-Trilon.io It will fire, cause it runs inside the zone. I don't see calling the subscribe inside runOutsideAngularSpreader
in the service constructor, you have to use .pipe() to use distinctUntilChanged(), pluck() and map(), both imported from rxjs/operators used like this: .pipe(map()). Also, just use fromEvent() imported directly from rxjs instead of Observable.fromEvent()Westleigh
W
13

I haven't seen anyone talking about MediaMatcher of angular/cdk.

You can define a MediaQuery and attach a listener to it - then anywhere on your template (or ts) you can invoke stuff if the Matcher is matched. LiveExample

App.Component.ts

import {Component, ChangeDetectorRef} from '@angular/core';
import {MediaMatcher} from '@angular/cdk/layout';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  mobileQuery: MediaQueryList;

  constructor(changeDetectorRef: ChangeDetectorRef, media: MediaMatcher) {
    this.mobileQuery = media.matchMedia('(max-width: 600px)');
    this._mobileQueryListener = () => changeDetectorRef.detectChanges();
    this.mobileQuery.addListener(this._mobileQueryListener);
  }

  private _mobileQueryListener: () => void;

  ngOnDestroy() {
    this.mobileQuery.removeListener(this._mobileQueryListener);
  }

}

App.Component.Html

<div [class]="mobileQuery.matches ? 'text-red' : 'text-blue'"> I turn red on mobile mode 
</div>

App.Component.css

.text-red { 
   color: red;
}

.text-blue {
   color: blue;
}

source: https://material.angular.io/components/sidenav/overview

Whole answered 17/2, 2018 at 13:44 Comment(0)
M
6

Assuming that < 600px means mobile to you, you can use this observable and subscribe to it:

First we need the current window size. So we create an observable which only emits a single value: the current window size.

initial$ = Observable.of(window.innerWidth > 599 ? false : true);

Then we need to create another observable, so that we know when the window size was changed. For this we can use the "fromEvent" operator. To learn more about rxjs`s operators please visit: rxjs

resize$ = Observable.fromEvent(window, 'resize').map((event: any) => {
  return event.target.innerWidth > 599 ? false : true;
 });

Merg these two streams to receive our observable:

mobile$ = Observable.merge(this.resize$, this.initial$).distinctUntilChanged();

Now you can subscribe to it like this:

mobile$.subscribe((event) => { console.log(event); });

Remember to unsubscribe :)

Montero answered 7/5, 2017 at 17:34 Comment(0)
E
6

If you want just one event after the resize is finished, it's better to use RxJS with debounceTime : debounceTime: Discard emitted values that take less than the specified time between output.

He waits > 0.5s between 2 events emitted before running the code. In simpler terms, it waits for the resizing to be finished before executing the next code.

// RxJS v6+
import { fromEvent } from 'rxjs';
import { debounceTime, map } from 'rxjs/operators';

...

const resize$ = fromEvent(window, 'resize');
    resize$
      .pipe(
        map((i: any) => i),
        debounceTime(500) // He waits > 0.5s between 2 events emitted before running the next.
      )
      .subscribe((event) => {
        console.log('resize is finished');
      });

StackBlitz example

StackBlitz

Exquisite answered 20/7, 2022 at 15:43 Comment(0)
J
5

I checked most of these answers. then decided to check out Angular documentation on Layout.

Angular has its own Observer for detecting different sizes and it is easy to implement into the component or a Service.

a simpl example would be:

import {BreakpointObserver, Breakpoints} from '@angular/cdk/layout';

@Component({...})
class MyComponent {
  constructor(breakpointObserver: BreakpointObserver) {
    breakpointObserver.observe([
      Breakpoints.HandsetLandscape,
      Breakpoints.HandsetPortrait
    ]).subscribe(result => {
      if (result.matches) {
        this.activateHandsetLayout();
      }
    });
  }
}

hope it helps

Jugurtha answered 14/5, 2020 at 1:35 Comment(1)
This is nice to know... but just a note... this is material library angular docs, not official documentation from Angular itself. Sort of suggestive that all the breakpoints will follow Material guidelines OOTB.Briticism
Q
4

This is not exactly answer for the question but it can help somebody who needs to detect size changes on any element.

I have created a library that adds resized event to any element - Angular Resize Event.

It internally uses ResizeSensor from CSS Element Queries.

Example usage

HTML

<div (resized)="onResized($event)"></div>

TypeScript

@Component({...})
class MyComponent {
  width: number;
  height: number;

  onResized(event: ResizedEvent): void {
    this.width = event.newWidth;
    this.height = event.newHeight;
  }
}
Quadri answered 15/1, 2018 at 11:34 Comment(2)
if you want to detect window resizes then Angular Material Breakpoint Observer handles already material.angular.io/cdk/layout/overviewDefiance
But this does not detect only windows resize, but any element resize.Quadri
H
3

Based on the solution of @cgatian I would suggest the following simplification:

import { EventManager } from '@angular/platform-browser';
import { Injectable, EventEmitter } from '@angular/core';

@Injectable()
export class ResizeService {

  public onResize$ = new EventEmitter<{ width: number; height: number; }>();

  constructor(eventManager: EventManager) {
    eventManager.addGlobalEventListener('window', 'resize',
      e => this.onResize$.emit({
        width: e.target.innerWidth,
        height: e.target.innerHeight
      }));
  }
}

Usage:

import { Component } from '@angular/core';
import { ResizeService } from './resize-service';

@Component({
  selector: 'my-component',
  template: `{{ rs.onResize$ | async | json }}`
})
export class MyComponent {
  constructor(private rs: ResizeService) { }
}
Haplology answered 10/12, 2017 at 13:48 Comment(2)
I found this as the best solution, however, this only works when the window is resized, but not when loads or when the router change. Do you know, how to apply with router change, reload or load?Courses
You can add a function to the service and trigger it in component @jcdsr: getScreenSize() { this.onResize$.emit({ width: window.innerWidth, height: window.innerHeight }); }Grayson
E
2

On Angular2 (2.1.0) I use ngZone to capture the screen change event.

Take a look on the example:

import { Component, NgZone } from '@angular/core';//import ngZone library
...
//capture screen changed inside constructor
constructor(private ngZone: NgZone) {
    window.onresize = (e) =>
    {
        ngZone.run(() => {
            console.log(window.innerWidth);
            console.log(window.innerHeight);
        });
    };
}

I hope this help!

Embarkation answered 3/3, 2017 at 7:10 Comment(0)
O
2

I wrote this lib to find once component boundary size change (resize) in Angular, may this help other people. You may put it on the root component, will do the same thing as window resize.

Step 1: Import the module

import { BoundSensorModule } from 'angular-bound-sensor';

@NgModule({
  (...)
  imports: [
    BoundSensorModule,
  ],
})
export class AppModule { }

Step 2: Add the directive like below

<simple-component boundSensor></simple-component>

Step 3: Receive the boundary size details

import { HostListener } from '@angular/core';

@Component({
  selector: 'simple-component'
  (...)
})
class SimpleComponent {
  @HostListener('resize', ['$event'])
  onResize(event) {
    console.log(event.detail);
  }
}
Odonto answered 15/8, 2017 at 13:9 Comment(0)
S
2

Below code lets observe any size change for any given div in Angular.

<div #observed-div>
</div>

then in the Component:

oldWidth = 0;
oldHeight = 0;

@ViewChild('observed-div') myDiv: ElementRef;
ngAfterViewChecked() {
  const newWidth = this.myDiv.nativeElement.offsetWidth;
  const newHeight = this.myDiv.nativeElement.offsetHeight;
  if (this.oldWidth !== newWidth || this.oldHeight !== newHeight)
    console.log('resized!');

  this.oldWidth = newWidth;
  this.oldHeight = newHeight;
}
Shivers answered 28/11, 2018 at 11:43 Comment(1)
I want to change a varibale from counter = 6 to count = 0 at max-width 767px, How do i do that ?Subtitle
C
1

Here is an update to @GiridharKamik answer above with the latest version of Rxjs.

import { Injectable } from '@angular/core';
import { Observable, BehaviorSubject, fromEvent } from 'rxjs';
import { pluck, distinctUntilChanged, map } from 'rxjs/operators';

@Injectable()
export class WindowService {
    height$: Observable<number>;
    constructor() {
        const windowSize$ = new BehaviorSubject(getWindowSize());

        this.height$ = windowSize$.pipe(pluck('height'), distinctUntilChanged());

        fromEvent(window, 'resize').pipe(map(getWindowSize))
            .subscribe(windowSize$);
    }

}

function getWindowSize() {
    return {
        height: window.innerHeight
        //you can sense other parameters here
    };
};
Cutlerr answered 10/7, 2020 at 12:54 Comment(0)
M
1

Here is a simple and clean solution I created so I could inject it into multiple components.

ResizeService.ts

import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class ResizeService {

  constructor() {

    window.addEventListener('resize', (e) => {
      this.onResize.next();
    });

  }

  public onResize = new Subject();

}

In use:

constructor(
  private resizeService: ResizeService
) { 

  this.subscriptions.push(this.resizeService.onResize.subscribe(() => {
    // Do stuff
  }));

}

private subscriptions: Subscription[] = [];
Moniz answered 13/8, 2020 at 1:54 Comment(0)
R
1

What I did is as follows, much like what Johannes Hoppe suggested:

import { EventManager } from '@angular/platform-browser';
import { Injectable, EventEmitter } from '@angular/core';

@Injectable()
export class ResizeService {

  public onResize$ = new EventEmitter<{ width: number; height: number; }>();

  constructor(eventManager: EventManager) {
    eventManager.addGlobalEventListener('window', 'resize',
      event => this.onResize$.emit({
        width: event.target.innerWidth,
        height: event.target.innerHeight
      }));
  }
  
  getWindowSize(){
    this.onResize$.emit({
    width: window.innerWidth,
    height: window.innerHeight
    });
  }
}

In app.component.ts:

Import { ResizeService } from ".shared/services/resize.service"
import { Component } from "@angular/core"

@Component({
  selector: "app-root",
  templateUrl: "./app.component.html",
  styleUrls: ["./app.component.css"]
})
export class AppComponent{
  windowSize: {width: number, height: number};
  
  constructor(private resizeService: ResizeService){
  }

  ngOnInit(){
    this.resizeService.onResize$.subscribe((value) => {
      this.windowSize = value;
    });
    
    this.resizeService.getWindowSize();
  }
}

Then in your app.component.html:

<router-outlet *ngIf = "windowSize?.width > 1280 && windowSize?.height > 700; else errorComponent">
</router-outlet>

<ng-template #errorComponent>
  <app-error-component></app-error-component>
</ng-template>
Reveal answered 29/4, 2021 at 5:54 Comment(0)
C
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

Carvel answered 28/9, 2021 at 15:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.