简体   繁体   English

页面加载时如何使用AngularJs调用Google Cloud Endpoints以获取用户信息(PICTURE,NAME ..)

[英]How to call Google Cloud Endpoints with AngularJs when the page Loads, to get user information(PICTURE, NAME..)

I am trying to make a post request using Google Cloud Endpoints and AngularJS when the page loads so I can get the user information and fill the profile picture, profile description and so on... 我正在尝试在页面加载时使用Google Cloud Endpoints和AngularJS发出发布请求,以便获取用户信息并填写个人资料图片,个人资料描述等。

I am able to run requests when pressing a button or something like that but can't call the google endpoints automatically when the page loads and that is whats I am trying to achieve. 我可以在按下按钮或类似按钮时运行请求,但是在页面加载时无法自动调用google端点,这就是我要实现的目标。

Below is the HTML part where the {{userPicture}} should've been loaded in the angular script: 以下是应在角度脚本中加载{{userPicture}}的HTML部分:

(HTML) (HTML)

          <div class="form-group">
            <label class="col-sm-3 control-label">Profile image</label>
            <div class="col-sm-9" ng-controller='initController'>
                <img src="{{userPicture}}" class="user-image-profile" alt="User Image">
            </div>
          </div>

(ANGULAR) (英语)

controllers.initController = function($scope, $http){

$scope.userForm = {
        "userEmail" : $.cookie('auth') 
            };

gapi.client.igardenendpoints.getProfile($scope.userForm).execute(function(resp) {
    $scope.$apply(function () {

    if (resp.error) {
        $scope.backmessage.messagetext = "GetProfile Error!"
        console.log("error");

             } else {


    if (resp.userEmail == "TEMPLATE"){

            $scope.backmessage.messagetext = "Error please try again!"


    }else{

        $scope.userPicture = 'https://filiperebollo1986.appspot.com/serve?blob-key=' + resp.profilePicKey;


    }

    }
       });

 });



 }

error 错误

I also tried to use the following: 我也尝试使用以下方法:

$scope.initData = function () { $ scope.initData = function(){

gapi.client.igardenendpoints.getProfile($scope.userForm)........... gapi.client.igardenendpoints.getProfile($ scope.userForm)...........

} }

and run the function at the end of the controller, like: 并在控制器末端运行该函数,例如:

$scope.initData(); $ scope.initData();

But both does not work, any help on that? 但是两者都不起作用,对此有帮助吗?

I will not be able to help you in 100% as I'm not using Google Cloud, but will try to do my best. 由于我没有使用Google Cloud,因此无法100%地为您提供帮助,但会尽力而为。

First of all, to get the data it's usually better to use services rather than do it in the controller. 首先,要获取数据,通常最好使用服务而不是在控制器中进行操作。

But anyway, your problem seems to be different. 但是无论如何,您的问题似乎有所不同。 In your HTML did you include your script and client API? 您在HTML中是否包含脚本和客户端API?

I was able to fix my problem and bellow is the solution: 我能够解决我的问题,下面是解决方案:

The problem was that at the moment of my call, the script may not have been loaded once I was using the "ng-app" directive directly on the body TAG. 问题是,在我打电话的那一刻,一旦我直接在body TAG上使用“ ng-app”指令,脚本可能就没有被加载。

Now I am injecting the angular module dinamicaly just after my API loading: 现在,我在加载API之后立即注入了角度模块:

                    function googleOnLoadCallback(){  
                        var apisToLoad = 1; // must match number of calls to gapi.client.load()  
                        var gCallback = function() {  
                            if (--apisToLoad == 0) {  
                                //Manual bootstraping of the application  
                                var $injector = angular.bootstrap(document, ['authModule']);  
                                console.log('Angular bootstrap complete ' + gapi);  
                            };  
                        };  
                        gapi.client.load('igardenendpoints', 'v12', gCallback, '//' + window.location.host + '/_ah/api');  
                    }

               </script>

               <script src="https://apis.google.com/js/client.js?onload=googleOnLoadCallback"></script> 

And now It is working!!!! 现在它正在工作!!!

The only problem now is that when the page loads it appears the {{example}} in the page, is it possible to avoid the {{}} to appear? 现在唯一的问题是,当页面加载时,页面中将显示{{example}},是否可以避免出现{{}}?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 带有Google Cloud Endpoints的AngularJS表单 - AngularJS forms with Google Cloud Endpoints 用户刷新页面时如何进行angularjs $ http POST调用? - How to make angularjs $http POST call when user refresh a page? 使用MVC,C#和AngularJS时如何在页面加载之间保留页面的下拉值? - How to retain a page's dropdown value across page loads when using MVC, C# and AngularJS? 当我刷新页面时,不会加载页面。 Angularjs + Nodejs - when I refresh the page, not the page loads. Angularjs + Nodejs 控件加载时如何在AngularJS中加载数据? - How to Load data in AngularJS when control loads? 在AngularJS中刷新页面时如何将用户重定向到登录页面 - How to redirect user to login page when refreshing the page in AngularJS 当用户单击页面上的任意位置时,如何自动使文本区域为空。 Angularjs,Javascript? - how to get text area empty automatically when user click anywhere on the page. Angularjs, Javascript? AngularJS两个控制器…在页面加载时克服了$ scope。$ on - AngularJS two controllers… overcoming $scope.$on when page loads 在页面加载之前和刷新时显示的内容,AngularJS - content showing before the page loads and when refreshing, AngularJS AngularJS-ng-change仅在页面加载时触发一次 - AngularJS - ng-change only firing once when the page loads
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM