2016-06-19 10 views
2

Angular2:私はウィンドウサイズ変更イベントを処理するソリューションを使用しています。しかし、私はイベントの値を取得していない。ターゲット値はウィンドウの幅を取得する。Angular2ウィンドウのサイズ変更イベントに目標値がありません

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

onResize(event) { 
    console.log(event); 
    console.log(event.target); 
} 

ログイン...

{"isTrusted":true} 
undefined 

ウィンドウに幅を取得する方法:イベントのサイズを変更しますか?

+0

奇妙な、理想的にそれが動作するはずです。あなたは 'event.currentTarget'を試しましたか? – codef0rmer

+0

はい。私には奇妙に見えます。 –

答えて

3

私にとってこの小さな例の作業:

import { Component } from '@angular/core'; 

@Component({ 
    selector: 'my-app', 
     template: ` 
     <h1 (window:resize)="onResize($event)">{{title}}</h1> 
     ` 
    }) 
    export class AppComponent { 
     title = 'Hello window object !'; 

     onResize(event) { 
     console.log(event); 
     console.log("width:" + event.target.innerWidth); 
     console.log("height:" + event.target.innerHeight); 
    } 
} 
+0

[** Plunker example **](https://plnkr.co/edit/95R1RAT43wW1KV2DHaQh?p=preview) –

+0

この例は正常に動作しています –

+0

これを実装する最善の方法は?ウィンドウイベントをキャプチャするかngZoneを使用しますか?何かご意見は。 –

1

これは私のために働きました。

constructor(ngZone:NgZone) { 
    super(); 
    window.onresize = (e) => 
     { 
      ngZone.run(() => { 
       this.callMethod(window.innerWidth); 
      }); 
     }; 

}

0

私はあなたがウィンドウのサイズ変更イベントを呼び出しているとして任意の目標値を必要としませんと思います。あなたにも以下のようにウィンドウのサイズを取得することができます。

コールイベント:

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

イベント:

onResize(event) { 
    console.log(window.innerWidth); 
    console.log(window.innerHeight); 
} 
関連する問題