Using ngCordova plugins without calling them in app.js?

My app is looking good, thanks to Ionic. All the core info is there and I’m just adding the frills - email, sharing, media (one of the functions is a metronome) and so on.

I can’t get any plugins to work.

I’ve had success with a previous Ionic app but the plugins were all called from within

.run(function($ionicPlatform) {
   $ionicPlatform.ready(function() {
   }
}

and indeed the Statusbar plugin seems to working fine and it is called from within there.

I’m using the Side Menu starter with tabs built in btw.

My issue, I suppose, is that I’ve three controller files.
main_ctrls.js - for the main app
menu_ctrls.js - for the menu pages like feedback and email, analytics
extras_ctrls.js - for the “extra” section with the metronome and so on.

I’ve put ‘ngCordova’ as a dependency in each module and called the plugin from within the controller with the ready function. Here is the email controller.

angular.module('menu.controllers', ['ngCordova'])

.controller('FeedCtrl', function($ionicPlatform, $scope, $cordovaEmailComposer) {

  $ionicPlatform.ready(function() {

    $cordovaEmailComposer.isAvailable().then(function() {
      // is available
      alert('Email is available');
    }, function () {
      // not available
      alert('Email is NOT available');
    });

    var email = {
      to: 'max@mustermann.de',
      cc: 'erika@mustermann.de',
      bcc: ['john@doe.com', 'jane@doe.com'],
      attachments: [
        'file://img/logo.png',
        'res://icon.png',
        'base64:icon.png//iVBORw0KGgoAAAANSUhEUg...',
        'file://README.pdf'
      ],
      subject: 'Cordova Icons',
      body: 'How are you? Nice greetings from Leipzig',
      isHtml: true
    };

    $cordovaEmailComposer.open(email).then(null, function () {
      alert('Email discarded.');
    });
  })
});

I’m testing it on Android (Nexus 4 with Android 5.1) with Chrome inspect and I just get an error saying “Cannot read property ‘isAvailable’ of undefined”

This happens with all plugins called from within controllers in this way.

What am I doing wrong?

Does your ng-cordova.js have the $cordovaEmailComposer, it may be a recent update you don’t have.

It doesn’t work with inAppBrowser or the Media plugin.

I can get both of those working without ngCordova at the moment though, then I’m moving on to the email.

At this point I’m really wondering what the point of ngCordova is, apart from to make us frustrated and our code 10 times more incomprehensible.

It’s a very handy Angular Wrapper around common Cordova plugins. Please post your ng-cordova.js file, or open it yourself, I bet you just have an outdated version.

I just downloaded it today!

Take a look at command issues here. See if any apply. I’d also open ng-cordova.js and look for isAvailable and make sure it’s there.

Also you only need to include ngCordova as a dependency once in your main angular module.

Try something like this:

// app.js
angular.module('yourapp', ['ionic', 'ngCordova', 'yourapp.controllers'])

.run(function(){
	// First thing that runs
});

angular.module('yourapp.controllers', []);


// main-ctrl.js
angular.module('yourapp.controllers')

.controller('MainCtrl', function($scope, $ionicPlatform, $cordovaEmailComposer) {

	$ionicPlatform.ready(function(){

		// Do Stuff with $cordovaEmailComposer

	});

});

// menu-ctrl.js
angular.module('yourapp.controllers')

.controller('MenuCtrl', function($scope) {

});

Yeah, I’ve read all that. And I’ve just checked my ng-cordova.js and isAvailable is there.

I think there is some instruction I’m missing, something I don’t get.

  1. I’ve got ng-cordova.js and the path is definitely correct…
  2. I’m able to run the sound and in-app browser plugins without ngCorvdova so the plugin install is fine.
  3. I’m including ngCordova as a dependency in my app.js angular.module and in the controller file angular.modules.
  4. I’ve tried running them from within an Ionic ready function.
  5. I have the latest version of everything.

I don’t know what the problem is.

Well I’ve ditched ngCordova and got the plugins working without it.

I don’t see why I’d need it. A useless headache imho.

You need to add cordova plugin:

ionic plugin add https://github.com/katzer/cordova-plugin-email-composer.git