为什么我无法使用 $cordovaCalendar 创建日历事件?

Why am I not able to create a calendar event using $cordovaCalendar?

我想在我的应用程序的设备日历中显示事件提醒,我正在尝试使用 ngCordova 的 $cordovaCalendar and the Phonegap Calendar Plugin 来实现这一点。使用 ngCordova 网站上的说明,我设置了 ngCordova 并安装了插件,但我 运行 遇到以下问题:

错误指向 ng-cordova.js 文件中的以下代码:

 $window.plugins.calendar.createEvent(
          defaultOptions.title,
          defaultOptions.location,
          defaultOptions.notes,
          new Date(defaultOptions.startDate),
          new Date(defaultOptions.endDate),
          function (message) {
            d.resolve(message);
          }, function (error) {
            d.reject(error);
          }
        );

我的控制器代码:

.controller('CalendarCtrl', function ($scope, $cordovaCalendar) {
  $cordovaCalendar.createEvent({
    title: 'Space Race',
    location: 'The Moon',
    notes: 'Bring sandwiches',
    startDate: new Date(2015, 0, 6, 18, 30, 0, 0, 0),
    endDate: new Date(2015, 1, 6, 12, 0, 0, 0, 0)
  }).then(function (result) {
    // success
  }, function (err) {
    // error
  });
})

在提供答案之前,我需要了解以下内容:

  • 您使用的是实际设备还是模拟器(不是浏览器)?
  • 您在 HTML?
  • 中引用了 cordova.js
  • 该插件已实际添加,证据显示在 config.xml?

这些问题通常是由于插件没有正确安装或者没有等待所有的Cordova插件加载引起的,我想你可能正在处理这里的第二个问题。添加 $ionicPlatform 作为依赖项并尝试以下操作:

$ionicPlatform.ready(function() {
  $cordovaCalendar.createEvent({
    title: 'Space Race',
    location: 'The Moon',
    notes: 'Bring sandwiches',
    startDate: new Date(2015, 0, 6, 18, 30, 0, 0, 0),
    endDate: new Date(2015, 1, 6, 12, 0, 0, 0, 0)
  }).then(function (result) {
    // success
  }, function (err) {
   // error
  });
});

$ionicPlatform.ready 确保在执行其范围内的任何已定义函数之前加载所有插件,否则您尝试在加载插件之前调用它。