我是 ionic 和 cordova 的新手,这个振动插件不工作。 能指出错误吗?
这是 index.html:
<body ng-app="starter">
<ion-pane>
<ion-header-bar class="bar-stable">
<h1 class="title">Ionic Blank Starter</h1>
</ion-header-bar>
<ion-content ng-controller="myCtrl">
<button ng-click="toggle()">Toggle</button>
</ion-content>
</ion-pane>
</body>
这是 js 文件夹中的 app.js:
var ionicApp = angular.module('starter', ['ionic', 'ngCordova'])
ionicApp.controller("myCtrl", function($scope, $cordovaVibration)) {
$scope.toggle = function() {
$ionicPlatform.ready(function() {
$cordovaVibration.vibrate(100).then(function() {
console.log("Phone vibrating");
})
});
}
}
.run(function($ionicPlatform) {
$ionicPlatform.ready(function() {
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
if (window.cordova && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
}
if (window.StatusBar) {
StatusBar.styleDefault();
}
});
})
我添加了 ng-cordova
振动插件。
最佳答案
我有同样的问题,我的解决了添加 Cordova 插件,看来你已经解决了这个问题:这是我与你的项目的不同之处:
我用的是这个版本的platform Ready
$scope.toggle = function() {
document.addEventListener( "deviceready", function() {
$cordovaVibration.vibrate( 2000 ); }, false );
};
我的 ionic 应用程序定义我首先放置了 ngCordova 连接。虽然这可能不是问题
var ionicApp = angular.module('starter', ['ngCordova','ionic' ])
为我解决问题的方法是正确安装插件:尝试:
cordova plugin add cordova-plugin-vibration
而不是官方网站说:
cordova plugin add org.apache.cordova.vibration
我将权限添加到/platforms/android/AndroidManifest.xm。虽然这似乎没有必要,因为我相信 ionic build 或 ionic run 发现你正在使用振动并为自己添加它
<uses-permission android:name="android.permission.VIBRATE" />
关于angularjs - ionic ng-cordova 振动插件不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30166861/