[英]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;
}
}
});
});
}
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.