Angular2获取窗口宽度onResize [英] Angular2 get window width onResize

查看:5671
本文介绍了Angular2获取窗口宽度onResize的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试弄明白如何获取窗口宽度调整Angular2中的事件。这是我的代码:

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

export class SideNav {

    innerWidth: number;

    constructor(private window: Window){

        let getWindow = function(){
          return window.innerWidth;
        };

        window.onresize = function() {
          this.innerWidth = getWindow();
          console.log(getWindow());
        };
} 

我在bootstrap.ts文件中全局导入窗口提供程序,访问我的应用程序到窗口。我所遇到的问题是,这给了我一个窗口大小,但在调整窗口大小 - 它只是重复相同的大小一遍又一遍,即使窗口改变大小。
查看console.log示例图片:
屏幕截图图片

I am importing window provider globally in the bootstrap.ts file using provide to gain access across my app to window. The problem I am having is this does give me a window size, but on resizing the window - it just repeats the same size over and over even if the window changes size. See image for console.log example: Screenshot image

我的总体目标是能够将窗口编号设置为一个变量onresize,所以我可以在模板中访问它。关于我在这里做错了什么想法?

My overall goal is to be able to set the window number to a variable onresize so I can have access to it in the template. Any ideas on what I am doing wrong here?

谢谢!

推荐答案



<div (window:resize)="onResize($event)"

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

这篇关于Angular2获取窗口宽度onResize的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆