Search code examples
cordovaionic-frameworkcalendarcordova-pluginsngcordova

Cannot read property 'calendar'


app.controller('programaCtrl', function( $scope, $state, $stateParams, $timeout,$rootScope, $cordovaCalendar) {
var programa = $stateParams.programa;

    $scope.programa = programa;

var item = {
        title: programa.titulo,
        location: programa.ubicacion,
        startDate: new Date(programa.fecha_inicio),
        endDate: new Date(programa.fecha_fin)
    };
        console.log(item);
var checkEvent = function(){
    $cordovaCalendar.findEvent(item)
        .then(function (result) {
            console.log('kkjk'+result);
            if (result.length.toString() == '0') {
                $scope.programa.calendario = 'Agregar al calendario +';
            } else {
                $scope.programa.calendario = 'Remover del calendario -';
            }
        },
        function (error) {
            alert('Ocurrio un problema al obtener los datos del calendario, por favor vuelve a intentar: ' + JSON.stringify(error));
});
}
$scope.addEvento = function() {
    if($scope.programa.calendario === 'Agregar al calendario +'){
        $cordovaCalendar.createEvent(item)
            .then(function (result) {
                checkEvent();
            },function (error) {
                alert('Ocurrio un problema al agregar al calendario, por favor vuelve a intentar: ' + JSON.stringify(error));
            });
    }else{
        $cordovaCalendar.deleteEvent(item)
            .then(function (result) {
                checkEvent();
            },
            function (error) {
                alert('Ocurrio un problema al borrar en el calendario, por favor vuelve a intentar: ' + JSON.stringify(error));
            });
    }
}
checkEvent();})

Someone could help me, my ionic app shows me this error!

TypeError:Cannot read property 'calendar' of undefined at object.findEvent(ng-Cordova.js:)


Solution

  • Install ngCordova

    $ bower install ngCordova
    

    Include ng-cordova.js or ng-cordova.min.js in your index.html file before cordova.js and after your AngularJS / Ionic file (since ngCordova depends on AngularJS).

    <script src="lib/ngCordova/dist/ng-cordova.js"></script>
    <script src="cordova.js"></script>
    

    include ngCordova as a dependency in your angular module

    angular.module('myApp', ['ngCordova'])