这适用于Ionic / Angular混合应用程序。我正在尝试使用cordova-plugin-shake启动shake event
。它应该重新加载随机图像。到目前为止,随机图像在加载时出现,但在模拟器中测试shake event
时它不起作用。
我收到错误ReferenceError: shake is not defined
然而,文档说You do not need to reference any JavaScript, the Cordova plugin architecture will add a shake object to your root automatically when you build.
我在这里错过了什么?我怎样才能让它发挥作用?我正在使用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
<!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>
答案 0 :(得分:2)
只有在构建和部署到真实手机时才会添加摇动对象。如果您在浏览器中进行测试,则不会。添加此项以避免测试时出现错误
if(window.shake) shake.startWatch(onShake,40);