将返回的auth2的值注入到angular变量中
Inject value of returned auth2 to angular variable
我已经找了一段时间,没有找到解决我的问题的方法,尽管我认为这可能很简单。
我使用angularJS和认证与谷歌auth2,以检索当前登录的用户。我想把用户名添加到一个angularJS变量中,只是为了在页面上显示它(至少现在是这样)。
这是我的相关html:
<meta name="google-signin-client_id" content="(((cut out my client id here))">
<script src="bower_components/angular/angular.js"></script>
<script src="https://apis.google.com/js/platform.js" async defer></script>
<body ng-app="CalendarApp" >
<div class="container" ng-controller="CalendarCtrl">
<div class="header well" >
<h1> Some heading </h1>
<p id="welcometext"> Welcome, {{userName}} </p>
</div>
</div>
</body>
和这里的相关js:
var myApp = angular.module('CalendarApp', []);
myApp.controller('CalendarCtrl', ['$scope', function($scope) {
$scope.userName = "tst";
gapi.load('auth2', function() {
auth2 = gapi.auth2.init({
fetch_basic_profile: true,
scope: 'profile'
});
// Sign the user in, and then retrieve their ID.
auth2.signIn().then(function() {
var profile = auth2.currentUser.get().getBasicProfile();
$("#welcometext").text("Welcome, " + profile.getName()); //this is my current workaround, but don't like it.
$scope.userName = profile.getName();
console.log($scope.userName); //this prints the correct name
});
}); // gapi.load
正如您可以从我的注释中得出的那样,auth调用工作并检索数据。我想从角度$scope.userName
来访问它。但是如果我在站点上显示它,它只显示" test ",而忽略$scope.userName = profile.getName();
语句。
我很确定这可能只是我的一个愚蠢的错误,但我已经在这上面花了很多时间了。
感谢任何帮助,谢谢!
要将auth2.signIn()
承诺转换为与AngularJS摘要循环集成的$q
服务承诺,请使用$q.when
:
gapi.load('auth2', function() {
auth2 = gapi.auth2.init({
fetch_basic_profile: true,
scope: 'profile'
});
// Sign the user in, and then retrieve their ID.
//auth2.signIn().then(function() {
//USE $q.when to integrate with AngularJS digest cycle
$q.when(auth2.signIn()).then(function() {
var profile = auth2.currentUser.get().getBasicProfile();
//$("#welcometext").text("Welcome, " + profile.getName()); //this is my current workaround, but don't like it.
$scope.userName = profile.getName();
console.log($scope.userName); //this prints the correct name
});
}); // gapi.load
通过将gapi
承诺转换为$q
服务承诺,AngularJS框架将通过.then
方法对$scope的变化做出反应。
From the Docs:
当美元将一个对象包装成一个
$q
promise,该对象可能是一个值或一个(第三方)可执行的promise。当你处理的对象可能是承诺,也可能不是承诺,或者承诺来自一个不可信的来源时,这是很有用的。
——AngularJS $q Service API参考- $q.when
你不需要这样做:
$("#welcometext").text("Welcome, " + profile.getName()); //this is my current workaround, but don't like
你所需要做的就是:
$scope.userName = profile.getName();
如果删除jQuery仍然不起作用,
写这个:
$scope.userName = profile.getName();
$timeout(function() {
$scope.$apply();
});
巴拉特。
相关文章:
- 访问脚本标记中的Angular变量
- angularjs:访问angular控制器中的jquery变量
- 带有变量的Angular.js数据源
- 如何将Jade变量分配给Angular绑定
- 如何使用 php 会话变量作为 Angular Js 数据
- Angular 1.5变量在调用一次之前不起作用
- 将angular变量{{$index+1}}转换为javascript整数
- Angular:如何在外部浏览器中打开Angular变量-appgyver混合应用程序
- 通过POST将Angular变量传递给Express后端
- 将返回的auth2的值注入到angular变量中
- 不能从Angular材质对话框调用的函数中访问Angular变量
- 如何从angular变量中拆分字符串值,并将其设置为两个html控件
- Angular变量没有在Blur-admin模板上更新
- Angular:变量模板内部指令
- 如何将angular 2变量's的值传递给javascript函数
- 完成上述函数之前设置的Angular变量i
- 插座IO消息不't更新Angular变量
- 如何将Angular变量注入JavaScript变量
- set Timeout中的Angular变量没有刷新
- Angular变量在改变后会恢复到初始值