2016-09-30 10 views
0

NativeScript + Angular 2を使い始めています。デバイスの向きに応じて異なるルートを起動するコンポーネントを実装したいと思います。多くの検索の後、私はこの作業を行うことができましたが、私はまだの最初のデバイスの方向を取得する方法を考え出していません。NativeScriptで角度2の初期デバイス方向を取得する方法

私のAppComponentコードです。 ngAfterViewInitを見てください:

import {Component, OnInit, OnDestroy, AfterViewInit} from "@angular/core"; 
import {Router} from "@angular/router"; 

import _application = require('application'); 

@Component({ 
    selector: "my-app", 
    templateUrl: "app.component.html", 
}) 
export class AppComponent implements OnInit, AfterViewInit, OnDestroy { 
    constructor(private router: Router) {} 

    ngOnInit() { 
     _application.on(_application.orientationChangedEvent, this.setOrientation.bind(this)); 
    } 

    ngAfterViewInit() { 
     // How do I get the initial orientation here instead of hardcoding to 'portrait'? 
     this.setOrientation({newValue: 'portrait'}) 
    } 

    ngOnDestroy() { 
     _application.off(_application.orientationChangedEvent, this.setOrientation); 
    } 

    setOrientation(args) { 
     if (args.newValue === 'landscape') { 
      this.router.navigate(['/landscape']); 
     } else { 
      this.router.navigate(['/portrait']); 
     } 
    } 
} 

答えて

2
private activity:any = application.android.foregroundActivity; 
this.activity.getResources().getConfiguration().orientation; 
+0

これは正しいですが、それは、Android固有のものです。私たちが[API](https://github.com/NativeScript/NativeScript/search?utf8=%E2%9C%93&q=DeviceOrientation)をエクスポートしていないようです。プルリクエストの良い機会です。 –

関連する問題