【问题标题】:how to add cordova\phonegap functionality in Ionic Framework Project如何在 Ionic 框架项目中添加 cordova\phonegap 功能
【发布时间】:2016-05-03 00:19:33
【问题描述】:

我正在研究离子框架(离子自己的生成器)。现在我想在我的应用中添加一些功能(例如,本地通知,或从移动相机拍照)。我的项目中有这个文件夹结构 根

挂钩

节点模块

平台(包括安卓)

scss

www

一些文件等,**

现在我如何将功能添加到我的项目中。我知道 cordova\phoneGap 提供了这些功能,但是如何添加它以及如何在我的控制器中使用这些插件?

【问题讨论】:

    标签: cordova plugins ionic-framework


    【解决方案1】:

    您可以使用 ngCordova。 http://ngcordova.com/

    本地通知

    module.controller('MyCtrl', function($scope, $cordovaLocalNotification) {
    
      $scope.addNotification = function () {
        $cordovaLocalNotification.add({
          id: 'some_notification_id'
          // parameter documentation:
          // https://github.com/katzer/cordova-plugin-local-notifications#further-informations-1
        }).then(function () {
          console.log('callback for adding background notification');
        });
      };
    
      $scope.cancelNotification = function () {
        $cordovaLocalNotification.cancel('some_notification_id').then(function () {
          console.log('callback for cancellation background notification');
        });
      };
    
      $scope.cancelAllNotification = function () {
        $cordovaLocalNotification.cancelAll().then(function () {
          console.log('callback for canceling all background notifications');
        });
      };
    
      $scope.checkIfIsScheduled = function () {
        $cordovaLocalNotification.isScheduled('some_notification_id').then(function (isScheduled) {
          console.log(isScheduled);
        });
      };
    
      $scope.getNotificationIds = function () {
        $cordovaLocalNotification.getScheduledIds().then(function (scheduledIds) {
          console.log(scheduledIds);
        });
      };
    
      $scope.checkIfIsTriggered = function () {
        $cordovaLocalNotification.isTriggered('some_notification_id').then(function (isTriggered) {
          console.log(isTriggered);
        });
      };
    
      $scope.getTriggeredIds = function () {
        $cordovaLocalNotification.getTriggeredIds().then(function (triggeredIds) {
          console.log(triggeredIds);
        });
      };
    
      $scope.notificationDefaults = $cordovaLocalNotification.getDefaults();
    
      $scope.setDefaultOptions = function () {
        $cordovaLocalNotification.setDefaults({ autoCancel: true });
      };
    
      // event callbacks events `onadd`, `ontrigger`, `onclick` and `oncancel`
      // can be assigned like this:
      $cordovaLocalNotification.onadd = function (id, state, json) {};
    
    });
    

    相机

    module.controller('PictureCtrl', function($scope, $cordovaCamera) {
    
      var options = {
        quality : 75,
        destinationType : Camera.DestinationType.DATA_URL,
        sourceType : Camera.PictureSourceType.CAMERA,
        allowEdit : true,
        encodingType: Camera.EncodingType.JPEG,
        targetWidth: 100,
        targetHeight: 100,
        popoverOptions: CameraPopoverOptions,
        saveToPhotoAlbum: false
      };
    
      $cordovaCamera.getPicture(options).then(function(imageData) {
        // Success! Image data is here
      }, function(err) {
        // An error occurred. Show a message to the user
      });
    
    });
    

    【讨论】:

    • 别忘了使用 CLI 命令添加 cordova 插件:cordova plugin add ...
    【解决方案2】:

    使用 ngCordova 是可选的,但它可以让您的应用程序井井有条,因为它通过 Angular DI(依赖注入)提供服务或指令。

    对于初学者,他们应该知道在普通版本中使用 Cordova 插件是了解 IOS 或 Android 平台原生功能使用的起点。请查看此站点http://www.plugreg.com/ 以检查可用的 Cordova 插件。

    【讨论】:

      猜你喜欢
      • 2013-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-14
      • 1970-01-01
      • 2011-08-20
      • 1970-01-01
      • 2017-01-30
      相关资源
      最近更新 更多