Angular2 get window width onResize

后端 未结 3 1912
广开言路
广开言路 2021-02-02 08:40

Trying to figure out how to get window width on resizing events in Angular2. Here is my code:

export class SideNav {

    innerWidth: number;

    constructor(pr         


        
3条回答
  •  -上瘾入骨i
    2021-02-02 09:19

    Use NgZone to trigger change detection:

    constructor(ngZone:NgZone) {
        window.onresize = (e) =>
        {
            ngZone.run(() => {
                this.width = window.innerWidth;
                this.height = window.innerHeight;
            });
        };
    }
    

提交回复
热议问题