【问题标题】:How to call Google Cloud Endpoints with AngularJs when the page Loads, to get user information(PICTURE, NAME..)如何在页面加载时使用 AngularJs 调用 Google Cloud Endpoints,以获取用户信息(图片、名称..)
【发布时间】:2016-09-10 02:13:31
【问题描述】:

我正在尝试在页面加载时使用 Google Cloud Endpoints 和 AngularJS 发出发布请求,以便获取用户信息并填写个人资料图片、个人资料描述等...

我可以在按下按钮或类似按钮时运行请求,但在页面加载时无法自动调用谷歌端点,这就是我想要实现的目标。

下面是应该在 Angular 脚本中加载 {{userPicture}} 的 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>

(角度)

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

我还尝试使用以下内容:

$scope.initData = function () {

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

}

并在控制器末尾运行函数,例如:

$scope.initData();

但两者都不起作用,有什么帮助吗?

【问题讨论】:

    标签: angularjs google-app-engine google-cloud-endpoints


    【解决方案1】:

    由于我没有使用 Google Cloud,因此我无法 100% 为您提供帮助,但会尽力而为。

    首先,要获取数据,通常最好使用服务而不是在控制器中进行。

    但无论如何,您的问题似乎有所不同。在您的 HTML 中是否包含您的脚本和客户端 API?

    【讨论】:

    • 你说得对,我将设法使用服务,而不仅仅是通过控制器调用我的 API。
    • 非常感谢您的回答,对我帮助很大。当你问我是否包括客户 KPI 时,我意识到在我打电话的那一刻,一旦我直接在 body TAG 上使用“ng-app”指令,脚本可能还没有加载。我将在这里发布解决方案。基本上,我在加载 API 后手动提升应用程序。
    • 我很高兴能帮助@FilipeRebollo :)
    【解决方案2】:

    我能够解决我的问题,下面是解决方案:

    问题是在我调用的那一刻,一旦我直接在 body TAG 上使用“ng-app”指令,脚本可能还没有加载。

    现在我在 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> 
    

    现在它正在工作!!!!

    现在唯一的问题是页面加载时出现页面中的{{example}},是否可以避免出现{{}}?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-20
      相关资源
      最近更新 更多