$ cordovaFile方法在调用时什么都不做? [英] $cordovaFile method does nothing when called?

查看:100
本文介绍了$ cordovaFile方法在调用时什么都不做?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Ionic应用程序中使用ngCordova插件,但似乎无法正常使用它们.这是我的控制器:

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


}]);

我没有收到任何错误代码或成功消息.它的作用就好像没有被调用一样,除非我将cordova.file.dataDirectory更改为我知道会破坏它的内容,例如数字.然后将触发错误警报.这是我的HTML:

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>

控制器正确地连接到$ scope上,ngCordova依赖项包含在我的app.js中,我想我的控制器函数中具有所有正确的注入方式.我可以看到实现这一点的任何想法或示例吗? docs 使它看起来非常易于使用,因此我必须缺少一些东西.

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.

推荐答案

好的,我想我已经解决了,但是我不知道为什么会这样.我取消了device.ready函数的功能,现在可以像这样使用它们:

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);
                })
        };

这篇关于$ cordovaFile方法在调用时什么都不做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆