2015-10-15 34 views
1

我嘗試過使用事件,但這不起作用。檢測離子應用程序中的方向更改

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

我知道如何獲得屏幕的方向,但我沒有找到如何檢測方向改變時如何檢測。

+0

我意識到這個問題有點舊,但正如我從Google在這裏指導的,當搜索「檢測scr在離子的方向變化「我認爲一個適當的答案也可以讓其他人受益! –

回答

2

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

對於科爾多瓦< 4運行:

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欲瞭解更多信息/文檔

4

普萊斯試試這個:

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

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

我試過了,但沒有在控制檯上顯示。 – Kreepz

+0

我應該把這個監聽器放在Ready()或Load中嗎? – Kreepz

+0

我編輯了答案,以顯示代碼的位置 –

相關問題