首页 > 解决方案 > 设备方向未按预期工作

问题描述

我正在使用 deviceorientation 侦听器尝试一些代码,但它没有像我预期的那样工作。我尝试了几种不同的方法(如下所示),并且在手动更改 Chrome 的调试器传感器时都可以正常工作,但是,我无法让它在任何移动设备上自动工作。

我将此站点用作参考和演示:

- https://www.audero.it/demo/device-orientation-api-demo.html

- https://code.tutsplus.com/tutorials/an-introduction-to-the-device-orientation-api--cms-21067

上面的演示在我的手机上运行良好,但是当我尝试在我正在处理的内容中实现相同的代码时,什么也没有发生。

export class AppComponent {
    absolute: boolean = false;
    alpha: number = 0;
    beta: number = 0;
    gamma: number = 0;
    private orientation$ = fromEvent(window, 'deviceorientation');

    constructor() { }

    ngAfterViewInit() {
    // Method 1
        window.addEventListener('deviceorientation', (e) => console.log(e));

    // Method 2
        this.orientation$.subscribe((res: any) => console.log(res));
    }
}

有什么我想念的想法吗?

标签: javascriptangulartypescriptrxjs

解决方案


推荐阅读