如何在Angular中触发窗口调整大小事件

如何在Angular中触发窗口调整大小事件

问题描述:

我在数据表上使用了Angular插件.数据表的容器是可调整大小的,而通过doc我了解到,当发生窗口调整大小事件时,数据表本身将自动调整大小.在jQuery中,我知道有一个$(window).trigger(),但我不知道该怎么做.

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?

您可以通过以下方式触发window.resize事件

You can trigger window.resize event by

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


要收听window.resize,请使用 HostListener ,如下所示:


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

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

实时演示

live demo