Ionic/Cordova: How to integrate Cordova Plugins into existing Ionic project?

后端 未结 7 2362
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-19 07:05

I have an Ionic project where I need the Cordova Camera plugin (which I now installed successfully). But in my project the Camera API is still not available, i.e. I get error th

7条回答
  •  醉话见心
    2021-02-19 07:22

    You need to inject Camera into the controller, like so:

    .controller('MyCtrl', function($scope, Camera) {
    

    Note that there is not a dollar sign before Camera. This really should be documented more explicitly.

    Also, you will need to add a factory to your services.js:

    .factory('Camera', ['$q', function($q) {
    
      return {
        getPicture: function(options) {
          var q = $q.defer();
    
          navigator.camera.getPicture(function(result) {
            // Do any magic you need
            q.resolve(result);
          }, function(err) {
            q.reject(err);
          }, options);
    
          return q.promise;
        }
      }
    }])
    

提交回复
热议问题