Cordova/Ionic - Set Portrait Only Mode for Mobiles

2019-06-20 09:02发布

问题:

How do we set Mobile phones to Portrait only and allow tablets to switch between portrait and landscape?

I am using Ionic/Cordova.

Currently I have configured to set the app to be in portrait mode in config.xml using the following property

 <preference name="orientation" value="portrait" />

回答1:

You could use isTablet plugin in conjunction with screen orientation plugin, something like:

.run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {
    if(window.isTablet){
        screen.unlockOrientation();
    }else{
        screen.lockOrientation('portrait');
    } 
  });
})