简体   繁体   中英

Ionic / Angular hybrid app, issue with getting a shake event working

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.

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.

Here's 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)];
};

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM