0

我嘗試使用下面的代碼,並使用$scope

var scopes = "https://www.googleapis.com/auth/contacts.readonly"; 

setTimeout(authorize(), 20); 

function authorize() { 
    gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: false}, handleAuthorization); 
} 
invitePeersController.gmailContacts = []; 
function handleAuthorization(authorizationResult) { 
    if (authorizationResult && !authorizationResult.error) { 
     $.get("https://www.google.com/m8/feeds/contacts/default/thin?alt=json&access_token=" + authorizationResult.access_token + "&max-results=50000&v=3.0", 
      function(response){ 
       //process the response here 
       console.log(response); 
       var jsonChildData = JSON.parse(JSON.stringify(response.feed.entry)); 
       for(var i=0; i<jsonChildData.length;i++){ 
        try { 
         var item = {}; 
         var name = JSON.stringify(jsonChildData[i].title.$t); 
         var email = JSON.stringify(jsonChildData[i].gd$email[0].address); 

         if(name.substring(1, name.length-1) && email.substring(1, email.length-1)){ 
         item ["name"] = name.substring(1, name.length-1); 
         item ["email"] = email.substring(1, email.length-1); 
         item ["id"] = email.substring(1, email.length-1).replace(/[^a-zA-Z ]/g, ""); 
         invitePeersController.gmailContacts.push(item); 
         } 
       } 
       catch(err) { 
       // console.log("Something went terribly wrong while trying to fetch Gmail Contacts Data"); 
       } 
      } 


      InvitePeersService.setGmailContactsData(invitePeersController.gmailContacts); 
        console.log(invitePeersController.gmailContacts); 
        $scope.$apply(function(){ 
         $scope.gmailData = invitePeersController.gmailContacts; 
         console.log($scope.gmailData); 
        }) 


       }); 
      } 
     } 

    } 

我可以得到$scope響應,但不能在其他地方獲得的數據。

如何在$scope中使用該值?

試圖按照this question,並應用$scope.$apply()但它不起作用。

+0

;',它記錄的數據? – anoop

+0

@anoop是的,它的確如此。 –

+0

在控制檯中是否有任何錯誤?你是在注入'$ scope'控制器?還是這是一個'service'?作爲一個建議,你應該使用'$ http.get()' – anoop

回答

0

您需要將以下模塊:

InvitePeersService.setGmailContactsData(invitePeersController.gmailContacts); 
console.log(invitePeersController.gmailContacts); 
$scope.$apply(function() { 
    $scope.gmailData = invitePeersController.gmailContacts; 
    console.log($scope.gmailData); 
}) 

function(response){ 

塊,使invitePeersController.gmailContacts初始化 - 作爲響應是在一個回調功能。

因此:

var scopes = "https://www.googleapis.com/auth/contacts.readonly"; 

setTimeout(authorize(), 20); 

function authorize() { 
    gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: false}, handleAuthorization); 
} 
invitePeersController.gmailContacts = []; 
function handleAuthorization(authorizationResult) { 
    if (authorizationResult && !authorizationResult.error) { 
    $.get('https://www.google.com/m8/feeds/contacts/default/thin?alt=json&access_token=' + authorizationResult.access_token + '&max-results=50000&v=3.0', 
     function (response) { 
     //process the response here 
     console.log(response); 
     var jsonChildData = JSON.parse(JSON.stringify(response.feed.entry)); 
     for (var i = 0; i < jsonChildData.length; i++) { 
      try { 
      var item = {}; 
      var name = JSON.stringify(jsonChildData[i].title.$t); 
      var email = JSON.stringify(jsonChildData[i].gd$email[0].address); 

      if (name.substring(1, name.length - 1) && email.substring(1, email.length - 1)) { 
       item ['name'] = name.substring(1, name.length - 1); 
       item ['email'] = email.substring(1, email.length - 1); 
       item ['id'] = email.substring(1, email.length - 1).replace(/[^a-zA-Z ]/g, ''); 
       invitePeersController.gmailContacts.push(item); 
      } 

      InvitePeersService.setGmailContactsData(invitePeersController.gmailContacts); 
      console.log(invitePeersController.gmailContacts); 
      $scope.$apply(function() { 
       $scope.gmailData = invitePeersController.gmailContacts; 
       console.log($scope.gmailData); 
      }) 
      } 
      catch (err) { 
      // console.log("Something went terribly wrong while trying to fetch Gmail Contacts Data"); 
      } 
     } 
     }); 
    } 
} 
當你`的console.log(invitePeersController.gmailContacts)
+0

仍然是一樣的。 –

+0

你在哪裏/如何顯示數據? – Ioan

+0

我想直接從$ scope顯示它 –

相關問題