2016-10-06 2 views
10

この解決策が見つかりました。それは有効ですか?typescriptを使ってangular2のデバイスの高さと幅を取得する方法は?

import {Component} from '@angular/core'; 
import {Platform} from 'ionic-angular'; 

@Component({...}) 
export MyApp { 
constructor(platform: Platform) { 
    platform.ready().then((readySource) => { 
    console.log('Width: ' + platform.width()); 
    console.log('Height: ' + platform.height()); 
    }); 
} 
} 

このソリューションは、イオン2 のためである私も、角度2を使用することができますか?

私に正しい方法を教えてください。

+1

質問に答えてください。これはAngular 2の有効な方法ではありません。なぜなら、IonicはAngularの一部ではありませんが、逆もまた可能です。あなたはまだ – JoeriShoeby

+0

@JoeriShoebyを「窓」を使用してクライアントの高さ/幅を取得することができよ: - はい、今私は 幅しようとしています:window.innerWidth、 高さ:window.innerHeight をしかし、私はエラーを取得しています。 [ts]名前空間 'window'が見つかりません。 –

+0

これは、Typescriptコンパイラが 'ウィンドウ'について知らないためです。おそらくコンパイラーがそれを認識できるようにタイピングをインストールする必要があります。 – JoeriShoeby

答えて

11

解決策が見つかりました。答えは非常に簡単です。あなたのコンストラクタに以下のコードを書いてください。

export class Dashboard { 
mobHeight: any; 
mobWidth: any; 
    constructor(private router:Router, private http: Http){ 
     this.mobHeight = (window.screen.height) + "px"; 
     this.mobWidth = (window.screen.width) + "px"; 
      console.log(this.mobHeight); 
      console.log(this.mobWidth) 
    } 
} 
20
export class Dashboard { 
    innerHeight: any; 
    innerWidth: any; 
    constructor(private router: Router, private http: Http) { 
     this.innerHeight = (window.screen.height) + "px"; 
     this.innerWidth = (window.screen.width) + "px"; 
    } 
} 
5

あなたは窓を注入することになるでしょう、このコンポーネントをテストするために欠けている場合に注意してください。 @Inject()関数を使用して、ウィンドウオブジェクトにこのような文字列トークンを使用して名前を付けることができます。duplicate

関連する問題