问题
I'm trying to use the ngCordova plugins in my Ionic app, but I can't seem to get them working properly. Here is my Controller :
.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
}]);
I'm not getting any error codes, or success messages. It acts like it's not even being called, UNLESS I change the cordova.file.dataDirectory
to something I know will break it, like a number. Then the error alert will fire. Here is my 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>
The controller is attached to the $scope correctly, the ngCordova dependency is included in my app.js, and I think I have all the correct injections in my controller function. Any ideas or examples I can see to implement this? The docs make it look very easy to use, so I must be missing something.
回答1:
OK, I think I fixed it but I don't know why this would work. I took the functions OUT of the device.ready function and now I can use them like this:
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);
})
};
来源:https://stackoverflow.com/questions/32056322/cordovafile-method-does-nothing-when-called