Ionic / Angular混合应用程序,获取一个摇动事件工作的问题 [英] Ionic / Angular hybrid app, issue with getting a shake event working

查看:188
本文介绍了Ionic / Angular混合应用程序,获取一个摇动事件工作的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个Ionic / Angular混合应用程式。我正在尝试使用摇动事件 > cordova-plugin-shake 。它应该重新加载一个随机图像。到目前为止,一个随机图像显示在负载,但它不工作时在模拟器中测试 shake事件

This is for a Ionic/Angular hybrid app. I'm trying get a shake event to fire using cordova-plugin-shake. It's supposed to reload a random image. So far a random image is showing up on load, but it's not working when testing a shake event in the simulator.

我收到错误 ReferenceError:shake未定义但是文档说你不需要引用任何JavaScript,Cordova插件架构将在您创建时自动向您的根添加一个shake对象。我在这里缺少什么?我如何让这个工作?我使用cordova版本6.3.0。

I'm getting the error ReferenceError: shake is not defined However the docs say You do not need to reference any JavaScript, the Cordova plugin architecture will add a shake object to your root automatically when you build. What am I missing here? How can I get this to work? I'm using cordova ver 6.3.0.

以下是app.js

angular.module('TarotApp', ['ionic','ngAnimate'])

.run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {
    if(window.cordova && window.cordova.plugins.Keyboard) {
      // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
      // for form inputs)
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);

      // Don't remove this line unless you know what you are doing. It stops the viewport
      // from snapping when text inputs are focused. Ionic handles this internally for
      // a much nicer keyboard experience.
      cordova.plugins.Keyboard.disableScroll(true);
    }
    if(window.StatusBar) {
      StatusBar.styleDefault();
    }
    shake.startWatch(onShake, 40);
  });
})

angular.module('TarotApp')
    .controller('TarotCtrl', function ($scope) {

        $scope.tarotImg = ['1', '2', '3', '4', '5', '6'];

        $scope.randTarotImg = $scope.tarotImg[Math.floor(Math.random() * $scope.tarotImg.length)];


});

var onShake = function () {
  $scope.randTarotImg = $scope.tarotImg[Math.floor(Math.random() * $scope.tarotImg.length)];
};

以下是index.html

Here's index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
    <title></title>

    <link href="lib/ionic/css/ionic.css" rel="stylesheet">
    <link href="css/style.css" rel="stylesheet">

    <!-- IF using Sass (run gulp sass first), then uncomment below and remove the CSS includes above
    <link href="css/ionic.app.css" rel="stylesheet">
    -->

    <!-- ionic/angularjs js -->
    <script src="lib/ionic/js/ionic.bundle.js"></script>
    <script src="lib/angular-animate/angular-animate.min.js"></script>

    <!-- cordova script (this will be a 404 during development) -->
    <script src="cordova.js"></script>

    <!-- your app's js -->
    <script src="js/app.js"></script>
  </head>
  <body ng-app="TarotApp">

    <ion-pane>

     <!--  <ion-header-bar class="bar-stable">

      </ion-header-bar>-->

      <ion-content ng-controller="TarotCtrl">


            <img ng-src="img/{{randTarotImg}}.jpg" class="tarot"/> 


      </ion-content>

    </ion-pane>



  </body>
</html>


推荐答案

到一个真正的手机。不是如果你在浏览器中测试。添加此项以避免测试时出错

The shake object only gets added when you build and deploy to a real phone. Not if you're testing in the browser. Add this to avoid the error when testing

if(window.shake) shake.startWatch(onShake,40);

这篇关于Ionic / Angular混合应用程序,获取一个摇动事件工作的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆