TypeError:无法读取属性'授权'在AngularJS应用程序中未定义

时间:2016-02-18 05:43:21

标签: javascript angularjs

我使用的工厂的代码可以从Google API获取日历事件数据。我有一个带有链接函数的指令,该函数调用运行该函数的工厂服务来从API获取日历事件数据。当我调用该服务时,我收到此错误:TypeError:无法读取属性'授权'未定义的。有人能告诉我为什么会收到此错误吗?当我记录" gapi"在我的控制台中,我可以看到" auth"对象属性,但我仍然未定义。我是否需要为角度使用一些特殊代码?以下是我的服务和指令的代码:

工厂:

var CLIENT_ID = '727304281402-2r6jn1l2jutllr07nc5ngba9omfqjand.apps.googleusercontent.com';
var SCOPES = ["https://www.googleapis.com/auth/calendar.readonly"];

app.factory('getCalendar', ['$q', function($q){
return function checkAuth() {
    console.log("Getting Calendar Events");
    var deferred = $q.defer();

    gapi.auth.authorize({
        'client_id': CLIENT_ID,
        'scope': SCOPES.join(' '),
        'immediate': true
    }, handleAuthResult);

    function handleAuthResult(authResult) {
        if (authResult && !authResult.error) {
          loadCalendarApi();
          console.log("You are authorized");
        } else {
          console.log("Not authorized due to " + authResult.error);
        }
    }

    function loadCalendarApi() {

        gapi.client.load('calendar', 'v3', listUpcomingEvents);
    }

    function listUpcomingEvents () {

        var request = gapi.client.calendar.events.list({
            'calendarId': 'magadesign.com_3337343339303832393534@resource.calendar.google.com',
            'orderBy': 'startTime',
            'singleEvents': true,
            'timeMin': (new Date()).toISOString(),
            'showDeleted': false,
            'maxResults': 3
        });

        request.then(function(response){

            var results = response.result.items;

            if(results === 0 ) {
                console.log('There are no events');
            }
            else {
                deferred.resolve(results);
            }
        });
    }
    return deferred.promise;

}

}]);

指令链接功能

app.directive('meetings', [ 'getCalendar', '$timeout', function(getCalendar, $timeout){
  return {
    restrict: 'E',
    templateUrl: 'scripts/directives/meetings.html',
    link: function(scope){
        getCalendar().then(function(events){
            scope.meetings = events;

            $timeout(function() {
                getCalendar();
            }, 60 * 1000 * 4);
        });

    }
  }
}]);

0 个答案:

没有答案