2016-06-27 86 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>頁面