簡體   English   中英

如何將index.html中的變量傳遞給服務?

[英]How to pass a variable in index.html to a service?

嘿,伙計們完成了菜鳥的准備。 你能幫助我嗎? 所以我有谷歌oauth提供的以下代碼:

<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

我希望能夠從有角的工廠訪問給定令牌。 這是通過$ scope嗎?

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

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


return service


}]);

謝謝

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

將其放入控制器中,並在html文件中使用ng-controller指令來使用{{id_token}}。

您可以通過回調或僅通過onSignIn()//it returns id_token解決此方法, onSignIn()//it returns id_token在我更改后onSignIn()//it returns id_token

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;
}

為您服務:

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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM