Cordova FileSystem in Service


I’m new to Ionic / AngularJS and I’m trying to parse an json file with the Cordova plugin FileSystem in a service and return its content. I’ve downloaded the file with File-Transfer to the Library folder of the iOS app.

Something is wrong with my code, but I can’t figure it out. Any help is appreciated.


myApp.controller('MensaMenuCtrl', function($scope, LoadData) {
    $ = LoadData.all();


myApp.factory('LoadData', function() {
    function onInitFs(fs) {
        fs.root.getFile('data.json', {}, function(fileEntry) {
            fileEntry.file(function(file) {
                var reader = new FileReader();
                reader.onloadend = function(e) {
                    var json = this.result;
                    var obj = JSON.parse(json);
                    return obj;

            }, errorHandler);
        }, errorHandler);
    function errorHandler(e) {

    return {
        all: function() {
            return window.requestFileSystem(window.PERSISTENT, 1024*1024, onInitFs, errorHandler);

Error from the console

Error: 'undefined' is not a function (evaluating 'window.requestFileSystem(window.PERSISTENT, 1024*1024, onInitFs, errorHandler)')


Are you sure window.requestFileSystem works?

It could be that Angular is already trying to load the plugin before Cordova’s deviceready function has triggered. You could try to:

1: Just to debug: try to wrap this particular function in a generous SetTimeout
2: Use Ionic’s version (actually a wrapper) of the Cordova deviceready function:

$ionicPlatform.ready(function() {
// Do your thing here


I’m sorry for my late reply and thanks for your help!

I’ve tried to put the code in the deviceready function but it doesn’t work in a service. It works perfectly fine in a .run() function. I’ve given up and now I’m using localStorage.

Thanks again!