Cordova:未定义 LocalFileSystem

Cordova: LocalFileSystem is not defined

我无法让 cordova 文件系统工作。我有一个具有以下依赖项的项目:

com.ionic.keyboard 1.0.3 "Keyboard"
org.apache.cordova.console 0.2.12 "Console"
org.apache.cordova.device 0.2.13 "Device"
org.apache.cordova.file 1.3.2 "File"
org.apache.cordova.file-transfer 0.4.8 "File Transfer"

app.js中我定义了对控制器模块的依赖:

angular.module('starter', ['ionic', 'controllers']);

控制器代码基本上是这样的:

angular.module('controllers', [])
  .controller('GalleryCtrl', function ($scope) {
    function success() {...}
    function error() {...}
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, success, error);
  }
}

然后我得到的是:

LocalFileSystem is not defined

此外,requestFileSystem 未定义。这种行为的原因可能是什么?

我正在使用 cordova 4.1.2 和 ionic 1.3.1。

编辑: 这是根据 html 标记:

<body ng-app="starter" ng-controller="GalleryCtrl">
<ion-nav-view>
    <ion-slide-box id="slideBox">
        <ion-slide ng-repeat="..."> <!-- details omitted -->
        </ion-slide>
    </ion-slide-box>
</ion-nav-view>
</body>

requestFileSystem 不可用的原因之一是设备未就绪。

在 window 准备就绪后尝试 运行 代码:

$scope.$on('$ionicView.enter', function(event, data) {
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, success, error);
});

我得到 LocalFileSystem 未在我的编辑器中定义,因为它是一个插件(根据我的理解),但是一旦 window 被加载,我就可以使用 requestFileSystem 并且 LocalFileSystem 会按预期工作。

您只是没有等待 deviceReady 事件被触发,因此文件插件尚未加载。更改

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, success, error);

document.addEventListener("deviceready", function() { 
  window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, success, error);
}, false);

LocalFileSystem.PERSISTENT 甚至在那之后可能仍未定义(在模拟等时对我来说一直如此)但它可以替换为 1 因为它只是一个常数。

对于离子注入 $ionicPlatform 然后使用 :

$ionicPlatform.ready(function() {       
   window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, success, error);
}, false);