1

私は NativeScript + Angular 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']);
        }
    }
}
4

3 に答える 3

2
private activity:any = application.android.foregroundActivity;
this.activity.getResources().getConfiguration().orientation;
于 2016-10-01T09:15:11.337 に答える