【问题标题】:$cordovaFile method does nothing when called?$cordovaFile 方法在调用时什么都不做?
【发布时间】:2015-11-10 10:34:10
【问题描述】:

我正在尝试在我的 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 让它看起来很容易使用,所以我一定错过了什么。

【问题讨论】:

  • 您如何测试您的应用程序?在浏览器中?模拟器?在设备上?
  • 我正在使用所有 3 种方法。浏览器、2 个模拟器,并在我的个人设备上运行。他们都有同样的问题。显然浏览器不适用于这个插件。
  • 因此,如果您将cordova.file.dataDirectory 更改为会破坏它的内容,然后单击“读取”按钮,会出现错误吗?
  • 它只影响我改变它的功能。因此,如果我更改 writeFile() 的路径,它将仅为该函数提供正确的错误响应。读取的响应仍然没有做任何事情。

标签: cordova ionic-framework ionic cordova-plugins ngcordova


【解决方案1】:

好的,我想我已经修好了,但我不知道为什么会这样。我从 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);
                })
        };

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多