cordova-plugin-screen-orientation不适用于特定页面

时间:2016-04-13 17:05:34

标签: javascript angularjs cordova ionic-framework

我只需要在一个视图上允许两个方向,但我的其他10个视图中的其余视图应该以纵向方式锁定。我不介意通过设置我的10个视图中的每个视图来做到这一点。控制器锁定肖像。但我似乎无法让屏幕方向的插件工作。

我使用CLI安装插件(成功安装)

然后这是代码

config.xml中:

<preference name="Orientation" value="default"/>

控制器:(这不起作用)

.controller('ProfileCtrl', function($scope, $state, $stateParams, $ionicHistory, $rootScope, $ionicPopup, $ionicPlatform) {

$ionicPlatform.ready(function() {
    screen.lockOrientation('landscape');
});

提前致谢:)

4 个答案:

答案 0 :(得分:1)

您应该先检查Cordova是否已加载。

  <!doctype html>
  <html>
  <head>
  <script>
  function postmessage() {
    var firstnames = ["John", "Jacob", ];
    var firstname = firstnames[Math.floor(Math.random() * firstnames.length)];


  function pickAnother(John) {
      if (John != firstnames) {
    run(sndnames);
      }
  }

  pickAnother (firstnames);

  var sndnames = ["Eric", "Conroy", ];
    var sndname = sndnames[Math.floor(Math.random() * sndnames.length)];

  </script>

  <meta charset="UTF-8">
  <title>Untitled Document</title>
  </head>

  <body>
  <input type="button" value="Get Recruit" onclick="postmessage();" />
  <div id="recruit"></div>
  </body>
  </html>

或者因为您正在使用Ionic:

document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady() {
    screen.lockOrientation('portrait');
}

(对于后者,请务必注入$ ionicPlatform依赖项。)

答案 1 :(得分:0)

使用以下代码,它将按预期工作

ALLSELECTED(DimDate)

答案 2 :(得分:0)

查看本教程。它帮助我解决了同样的问题: this

// Lock Device Orientation
function lockDeviceOrientationCallback() {
    // set to either landscape
    screen.orientation.lock('portrait');
    console.log("locking device orientation...");
}
document.addEventListener("deviceready", lockDeviceOrientationCallback, false);

该解决方案类似于以上在设备准备就绪时设置事件侦听器的答案。我发现此方法效果更好,因为Cordova已经有了一个可随时使用的设备侦听器,而且我似乎还喜欢上它。

答案 3 :(得分:0)

我将其放在“时间间隔”中并且有效:

Rectangle