2015-09-01 56 views
0

嘿傢伙完成noob角。你可以幫我嗎? 所以我有谷歌的OAuth提供以下代碼:如何將index.html中的變量傳遞給服務?

<script> 
    function onSignIn(googleUser) { 
    // Useful data for your client-side scripts: 
    var profile = googleUser.getBasicProfile(); 
    console.log("ID: " + profile.getId()); // Don't send this directly to your server! 
    console.log("Name: " + profile.getName()); 
    console.log("Image URL: " + profile.getImageUrl()); 
    console.log("Email: " + profile.getEmail()); 

    // The ID token you need to pass to your backend: 
    var id_token = googleUser.getAuthResponse().id_token; 
    console.log("ID Token: " + id_token); 
    }; 
</script> 

這是index.html的

我希望能夠從一個工廠,我在角訪問給定的令牌。這是通過$範圍?

App.factory('ranService', ['$http', '$q', function ($http, $q) { 

var service = {}; 
var randomUrl = "http://zzz; 
var googleToken; 


return service 


}]); 

感謝

+0

你能向我們展示角服務? –

+0

@Beto。檢查我的答案。如果它沒有幫助,請告訴我 –

回答

0
$scope.id_token = googleUser.getAuthResponse().id_token; 

把這個控制器並採用NG-控制器內部指令在HTML文件中使用{{id_token}}。

0

可以解決通過回調或我的變化

var profile; 
function onSignIn(googleUser, callback) { 
    // Useful data for your client-side scripts: 
    if (googleUser) 
     profile = googleUser.getBasicProfile(); 
    console.log("ID: " + profile.getId()); // Don't send this directly to your server! 
    console.log("Name: " + profile.getName()); 
    console.log("Image URL: " + profile.getImageUrl()); 
    console.log("Email: " + profile.getEmail()); 

    // The ID token you need to pass to your backend: 
    var id_token = googleUser.getAuthResponse().id_token; 
    console.log("ID Token: " + id_token); 
    //if there isn't success callback this callback use there 
    if (callback) 
     callback(id_token); 
    return id_token; 
} 

在服務後僅onSignIn()//it returns id_token這種方法:

angular.module('serviceModule') 
     .service('Service', ['$q', '$http', 
      function ($q, $http) { 

       function sendRequest() { 

        onSignIn(null, function (id_token) { 
         //you use this 
        }) 
        //or just 
        var idToken = onSignIn();//it can return without params 
       } 

       return { 
        sendRequest: sendRequest 
       } 
      }]); 

在控制器:

Service.sendRequest();