Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trigger window resize event in Angular

I am using a Angular plugin on data table. The container of the data table is resizable, while through the doc I learned that the data table it self will resize when there is a window resize event. In jQuery I know there is a $(window).trigger() but I don't know how to do that in angular.

My question is: in parent angular directive, how do I trigger that window resize event?

like image 649
Zanecat Avatar asked Aug 02 '17 04:08

Zanecat


People also ask

How do I capture a Windows resize event?

Answer: Use the addEventListener() Method You can simply use the addEventListener() method to register an event handler to listen for the browser window resize event, such as window. addEventListener('resize', ...) . The following example will display the current width and height of the browser window on resize.


1 Answers

You can trigger window.resize event by

window.dispatchEvent(new Event('resize'));

And for listening to window.resize, use HostListener as below example:

@HostListener('window:resize', ['$event'])
sizeChange(event) {
  console.log('size changed.', event);
}

live demo

like image 112
Pengyy Avatar answered Oct 04 '22 23:10

Pengyy