我试图在我的Ionic应用程序中使用ngCordova插件,但我似乎无法让它们正常工作。这是我的控制器:
.controller('InspectionCtrl', ['$scope', '$stateParams', '$cordovaDevice', '$ionicPlatform', '$cordovaFile', function($scope, $stateParams, $cordovaDevice, $ionicPlatform, $cordovaFile){
document.addEventListener("deviceready", function () {
//When save button is clicked, call this function
$scope.save = function() {
$cordovaFile.writeFile(cordova.file.dataDirectory, 'myFile.txt', "$scope.data", true)
.then(function(success){
alert('file created');
}, function(error){
alert('did not create file ' + error.code);
});
};
$scope.read = function() {
$cordovaFile.checkFile(cordova.file.dataDirectory, 'myFile.txt')
.then(function(success) {
alert(success);
}, function(error){
alert(error.code);
})
};
}, false); //end device ready
}]);
我没有收到任何错误代码或成功消息。除非我将cordova.file.dataDirectory
更改为我知道会破坏它的东西,就像一个数字一样。然后将触发错误警报。这是我的HTML:
<div class="item">
<div class="buttons">
<button class="button button-positive button-full" ng-click="save()">Save</button>
</div>
<div class="buttons">
<button class="button button-positive button-full" ng-click="read()">Read</button>
</div>
</div>
控制器正确连接到$ scope,ngCordova依赖项包含在我的app.js中,我想我的控制器功能中有所有正确的注入。我能看到实现这个的任何想法或例子吗? docs使它看起来非常容易使用,所以我必须遗漏一些东西。
答案 0 :(得分:0)
好的,我想我修好了但是我不知道为什么会这样。我使用了device.ready函数的函数,现在我可以像这样使用它们:
document.addEventListener("deviceready", function () {
var device = $cordovaDevice.getDevice();
var cordova = $cordovaDevice.getCordova();
var model = $cordovaDevice.getModel();
var platform = $cordovaDevice.getPlatform();
var uuid = $cordovaDevice.getUUID();
$scope.deviceID = $cordovaDevice.getUUID();
$cordovaFile.getFreeDiskSpace()
.then(function (success) {
// success in kilobytes
$scope.freeSpace = success;
}, function (error) {
// error
$scope.freeSpace = 'did not get free space...';
});
var version = $cordovaDevice.getVersion();
}, false); //end device ready
$scope.save = function() {
$cordovaVibration.vibrate(1000);
var inspection = JSON.stringify($scope.inspection);
$cordovaFile.writeFile(cordova.file.dataDirectory, 'myFile.txt', inspection, true)
.then(function(success){
alert(JSON.parse(inspection));
}, function(error){
alert('did not create file ' + error.code);
});
};
$scope.read = function() {
$cordovaFile.checkFile(cordova.file.dataDirectory, 'myFile.txt')
.then(function(success) {
alert('found it!');
}, function(error){
alert('didn\'t find the file: ' + error.code);
})
};