$cordovaFile plugin error

Hi all,

I want to use the cordova file plugin but I’m not able to.
I add the plugin using:

ionic plugin add org.apache.cordova.file

When I simple add $cordovaFile there:

angular.module('starter.controllers', []) 
.controller('AppCtrl', function($scope, $state, $location, $ionicModal, $timeout, $cordovaFile, speakerPass, beaconPass) {

I found this error:

    Error: [$injector:unpr] Unknown provider: $cordovaFileProvider <- $cordovaFile <- AppCtrl
http://errors.angularjs.org/1.3.13/$injector/unpr?p0=%24cordovaFileProvider%20%3C-%20%24cordovaFile%20%3C-%20AppCtrl
    at file:///android_asset/www/lib/ionic/js/ionic.bundle.js:8055:20
    at file:///android_asset/www/lib/ionic/js/ionic.bundle.js:12033:27
    at Object.getService [as get] (file:///android_asset/www/lib/ionic/js/ionic.bundle.js:12187:53)
    at file:///android_asset/www/lib/ionic/js/ionic.bundle.js:12038:53
    at getService (file:///android_asset/www/lib/ionic/js/ionic.bundle.js:12187:53)
    at invoke (file:///android_asset/www/lib/ionic/js/ionic.bundle.js:12217:78)
    at Object.instantiate (file:///android_asset/www/lib/ionic/js/ionic.bundle.js:12234:37)
    at file:///android_asset/www/lib/ionic/js/ionic.bundle.js:16545:38
    at self.appendViewElement (file:///android_asset/www/lib/ionic/js/ionic.bundle.js:47977:42)
    at Object.switcher.render (file:///android_asset/www/lib/ionic/js/ionic.bundle.js:46216:65) 

What I’m wrong?

Thank you.

Did you add ngCodova?
$cordovaFile is part of ngCordova.
And your error is saying that the dependency $cordovaFile wasn’t found.

How to install: http://ngcordova.com/docs/install/

Thank so much, I missed this.

Now all works on my phone.

I’ve tried the cordovaFile.checkFile function inside of ionic.Platform.ready and all goes well.

ionic.Platform.ready(function() {
    // will execute when device is ready, or immediately if the device is already ready.

    $cordovaFile.checkFile(cordova.file.dataDirectory, "some_file.txt")
        .then(function(success) {
            // success
        }, function(error) {
            alert("file directory: " + cordova.file.dataDirectory)
                // error
        });
    console.log("ready");
});

But if I try to use the same function in another controller I got this error (on my phone):

  TypeError: Cannot read property 'dataDirectory' of undefined
    at new <anonymous> (file:///android_asset/www/js/controllers.js:142:44)
    at invoke (file:///android_asset/www/lib/ionic/js/ionic.bundle.js:12110:17)
    at Object.instantiate (file:///android_asset/www/lib/ionic/js/ionic.bundle.js:12118:27)
    at file:///android_asset/www/lib/ionic/js/ionic.bundle.js:16387:28
    at self.appendViewElement (file:///android_asset/www/lib/ionic/js/ionic.bundle.js:47357:24)
    at Object.switcher.render (file:///android_asset/www/lib/ionic/js/ionic.bundle.js:45614:41)
    at Object.switcher.init (file:///android_asset/www/lib/ionic/js/ionic.bundle.js:45534:20)
    at self.render (file:///android_asset/www/lib/ionic/js/ionic.bundle.js:47231:14)
    at self.register (file:///android_asset/www/lib/ionic/js/ionic.bundle.js:47189:10)
    at updateView (file:///android_asset/www/lib/ionic/js/ionic.bundle.js:52439:23) <ion-nav-view name="AppContent" class="view-container" nav-view-transition="android"> 

This is my controller:

.controller('HomeCtrl', function($scope, $stateParams, $cordovaFile) {
        $cordovaFile.checkFile(cordova.file.dataDirectory, "some_file.txt")
            .then(function(success) {
                // success
            }, function(error) {
                alert("file directory: " + cordova.file.dataDirectory)
                    // error
            });
})

if it works for the first use case, it should work for the other case.
i assume its because controller does not wait for ionic ready event.

try in the controller

$ionicPlatform.on('platformready', function() {
   ...put your checkFile logic here
})

if this fix does not help you, please try to use safari debugger
(for example read file from interactive console and check if there is an error)