【问题标题】:Detect orientation change in Ionic App检测 Ionic App 中的方向变化
【发布时间】:2015-10-15 08:47:53
【问题描述】:

我尝试过使用事件,但这不起作用。

document.addEventListener('onorientationchange', function() 
   {...}
);

我知道如何获取屏幕的方向,但我不知道如何检测方向何时发生变化。

【问题讨论】:

  • 我意识到这个问题有点老了,但是当我在搜索“检测 Ionic 中的屏幕方向变化”时从谷歌那里得到指示时,我认为正确的答案也可能对其他人有益!

标签: ionic orientation detect


【解决方案1】:

请试试这个:

angular.module('starter', ['ionic'])

.run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {
    ...
    window.addEventListener("orientationchange", function() {
      console.log(window.orientation);
    }, false);  
  });
})

【讨论】:

  • 我应该把这个监听器放在 Ready() 还是 Load 中?
  • 我已经尝试了您的代码,但控制台中没有出现任何内容。 ://
【解决方案2】:

首先你需要安装cordova-plugin-screen-orientation插件:

对于科尔多瓦

cordova plugin add net.yoik.cordova.plugins.screenorientation

对于科尔多瓦 > 4 次运行:

cordova plugin add cordova-plugin-screen-orientation

插件将以下内容添加到屏幕对象(window.screen):

// lock the device orientation
.lockOrientation('portrait')

// unlock the orientation
.unlockOrientation()

// current orientation
.orientation

现在您可以使用类似以下的方法来检测屏幕的方向:

window.addEventListener("orientationchange", function(){
    console.log(screen.orientation); // e.g. portrait
});

有关更多信息/文档,请参阅https://github.com/gbenvenuti/cordova-plugin-screen-orientation

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-24
    • 2015-07-08
    • 1970-01-01
    相关资源
    最近更新 更多