2016-06-27 56 views
3

我试图使用Angular Js导入用户的Gmail联系人。该代码工作正常在纯JavaScript,但在角js错误。用angularjs导入Google联系人

HTML代码..

<a class="btn btn-primary btn-simple" ng-click="importgoogle()"><u>Import Gmail Friends</u></a> 

角代码..

var clientId = 'Client ID'; 
var scopes = 'https://www.googleapis.com/auth/contacts.readonly'; 
    $scope.importgoogle = function(){ 
    window.setTimeout(authorize);  //calls authorize() 
} 

var authorize = function(){ 
    gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: false}, handleAuthorization);  //calls handleAuthorization() 
} 

var handleAuthorization = function(){ 
    if (authorizationResult && !authorizationResult.error) { 
      $.get("https://www.google.com/m8/feeds/contacts/default/thin?alt=json&access_token=" + authorizationResult.access_token + "&max-results=500&v=3.0", 
      function(response){ 
       console.log(response); 
       }); 
     } 
} 

输入用户的ID &密码显示在控制台以下错误消息后..

Uncaught ReferenceError: authorizationResult is not defined 

由于此代码在Javascript.Pl中工作,无法理解我要出错的位置缓解帮助..

+0

你确定它是运行的代码吗?在这个代码中它不能得到这个错误。 – Ygalbel

+0

也许你必须刷新/重新加载你的代码 – Ygalbel

回答

0

问题在于handleAuthorization函数。实现该功能的正确方法是..

var handleAuthorization = function(authorizationResult){   //authorizationResult needs to be passed as an arguement. 
if (authorizationResult && !authorizationResult.error) { 
     $.get("https://www.google.com/m8/feeds/contacts/default/thin?alt=json&access_token=" + authorizationResult.access_token + "&max-results=500&v=3.0", 
     function(response){ 
      console.log(response); 
      }); 
    } 

}

使这一变化的角度JS代码现在能正常使用后。

2

下面是使用角的js工作实例:

app.controller("importGCCtrl", function($scope, $http) { 
$scope.config = { 
    'client_id': 'Client ID', 
    'scope': 'https://www.google.com/m8/feeds' 
}; 

$scope.inviteContacts = function() { 
    gapi.auth.authorize($scope.config, function() { 
     $scope.fetch(gapi.auth.getToken()); 
    }); 
} 

$scope.fetch = function(token) { 
    $http.get("https://www.google.com/m8/feeds/contacts/default/full?access_token=" + token.access_token + "&alt=json").then(function(response) { 
     console.log(response); 
     //console.log(response.data.feed.entry); 
     //$scope.contacts = response.data.feed.entry; // to assign data 
    }); 
} 

});

*注意:请确认您已经包含了API - <script src="https://apis.google.com/js/client.js"></script>页面