$cordovaFile method does nothing when called?

给你一囗甜甜゛ 提交于 2020-01-06 02:14:15

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!