Cordova:LocalFileSystem未定义

Bas*_*ian 7 cordova html5-filesystem cordova-plugins ionic

我无法使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"
Run Code Online (Sandbox Code Playgroud)

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

angular.module('starter', ['ionic', 'controllers']);
Run Code Online (Sandbox Code Playgroud)

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

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

然后我得到的是:

LocalFileSystem is not defined
Run Code Online (Sandbox Code Playgroud)

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

我使用的是cordova 4.1.2和离子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>
Run Code Online (Sandbox Code Playgroud)

Roo*_*nen 10

您只是不等待触发deviceReady事件,因此尚未加载File插件.更改

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, success, error);
Run Code Online (Sandbox Code Playgroud)

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

LocalFileSystem.PERSISTENT甚至可能后是不确定的(一直是我,而仿真等),但它可以替换为1,因为它仅仅是一个常数.

  • 请注意,由于这个问题的标题,像我这样的人可能会来这里,他们只是没有添加cordova文件插件,解决方案:`cordova插件添加cordova-plugin-file` (2认同)