Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular window resize event

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.

like image 959
DanAbdn Avatar asked Feb 20 '16 18:02

DanAbdn


1 Answers

<div (window:resize)="onResize($event)" 
onResize(event) {   event.target.innerWidth; } 

or using the HostListener decorator:

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

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.

like image 168
Günter Zöchbauer Avatar answered Sep 19 '22 00:09

Günter Zöchbauer