Access to cordova plugins from an ionic controller

I want to use Cordova Email Plugin in an ionic application.

I can access the plugin in the $ ionicPlatform function, but when I moved my code to my controller in the function for the ng-click descriptor, this does not work.

Know that my question is: how to access the cordova plugin objects from the controller?

This is my code that I use in app.js and it worked:

.run(function($ionicPlatform) { $ionicPlatform.ready(function() { cordova.plugins.email.isAvailable( function (isAvailable) { alert('Service is available'); } ); // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard // for form inputs) if(window.cordova && window.cordova.plugins.Keyboard) { cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true); } if(window.StatusBar) { // org.apache.cordova.statusbar required StatusBar.styleDefault(); } }); }) 

This is the same code in the .js controller that did not work (I installed my controller in my route):

 .controller('ApplicationController', function($scope) { $scope.sendMail = function () { cordova.plugins.email.isAvailable( function (isAvailable) { alert('Service is not available'); } ); } }) 

and this is my click:

  <a ng-click="sendMail()"> <i class="icon ion-android-forums"></i> send mail </a> 

This code works well in $ ionicPlatform, but does not work in a separate controller file.

+6
source share
2 answers

To do this, you must use the Angular.js wrapper.

Check out ngCordova .

0
source

Try using window.cordova instead of cordova when accessing the plugin outside of $ionicPlatform .

-1
source

Source: https://habr.com/ru/post/977844/


All Articles