有时不显示AngularJS中的Google登录按钮

艾玛

我点击了此链接https://developers.google.com/identity/sign-in/web/sign-in,以在基于Angular的网站上获取Google登录。

我已经看到一些奇怪的行为。登录按钮有时会显示,但并不总是显示。当我刷新页面时,只有五分之一的刷新,该按钮出现。

我在Chrome和Safari中尝试过,两者的行为相同。

码:

index.html

<script src="https://apis.google.com/js/platform.js" async defer></script>

<meta name="google-signin-client_id" content="my_client_id">

login.html

<div class="g-signin2" data-onsuccess="onSignIn"></div>  

login.js

angular.module('app').controller('LoginCtrl', function($scope) {
    window.onSignIn = function(googleUser) {
        // Get some info
    }
});
游泳汉堡

我的猜测是platform.js脚本等待DOM就绪事件,然后使用“ g-signin2”类查找div。但是,在Angularjs中,工作原理有所不同。之所以有时起作用,是因为有时div已由Angular渲染,有时未在Dom-ready事件之前渲染。有关如何使用您自己的javascript获得相同结果文档我做了一个遵循您的路由的示例。

<!doctype html>
<html lang="en" ng-app="app">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <div ng-view></div>

<script src="https://apis.google.com/js/platform.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.6/angular.min.js"></script>
<script src="https://code.angularjs.org/1.4.6/angular-route.min.js"></script>
<script>
    angular.module('app',['ngRoute'])
            .config(['$routeProvider',function($routeProvider){
                $routeProvider
                        .when('/log-in', {
                            template: '<button ng-click="logInCtrl.onLogInButtonClick()">Log In</button>',
                            controller: 'LogInController',
                            controllerAs: 'logInCtrl'
                        }).otherwise({
                            redirectTo:'/log-in'
                        });
            }])
            .controller('LogInController',function(){
                var self = this; //to be able to reference to it in a callback, you could use $scope instead
                gapi.load('auth2', function() {//load in the auth2 api's, without it gapi.auth2 will be undefined
                    gapi.auth2.init(
                            {
                                client_id: 'CLIENT_ID.apps.googleusercontent.com'
                            }
                    );
                    var GoogleAuth  = gapi.auth2.getAuthInstance();//get's a GoogleAuth instance with your client-id, needs to be called after gapi.auth2.init
                    self.onLogInButtonClick=function(){//add a function to the controller so ng-click can bind to it
                        GoogleAuth.signIn().then(function(response){//request to sign in
                            console.log(response);
                        });
                    };
                });
            });
</script>
</body>
</html>

或作为指令:

<!doctype html>
<html lang="en" ng-app="app" ng-controller="MainController">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <google-sign-in-button on-sign-in="onSignIn(response)" g-client-id="CLIENTID.apps.googleusercontent.com"></google-sign-in-button>

<script src="https://apis.google.com/js/platform.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.6/angular.min.js"></script>
<script>
    angular.module('app',[])
            .controller('MainController',['$scope', function ($scope) {
                $scope.onSignIn=function(response){
                    console.log(response);
                }
            }])
            .directive('googleSignInButton',function(){
                return {
                    scope:{
                        gClientId:'@',
                        callback: '&onSignIn'
                    },
                    template: '<button ng-click="onSignInButtonClick()">Sign in</button>',
                    controller: ['$scope','$attrs',function($scope, $attrs){
                        gapi.load('auth2', function() {//load in the auth2 api's, without it gapi.auth2 will be undefined
                            gapi.auth2.init(
                                    {
                                        client_id: $attrs.gClientId
                                    }
                            );
                            var GoogleAuth  = gapi.auth2.getAuthInstance();//get's a GoogleAuth instance with your client-id, needs to be called after gapi.auth2.init
                            $scope.onSignInButtonClick=function(){//add a function to the controller so ng-click can bind to it
                                GoogleAuth.signIn().then(function(response){//request to sign in
                                    $scope.callback({response:response});
                                });
                            };
                        });
                    }]
                };
            });
</script>
</body>
</html>

写完前面的示例后,我发现了一种更好,更轻松的方法来实现它。使用此代码,您可以像平常一样继承相同的按钮。

<!doctype html>
<html lang="en" ng-app="app" ng-controller="MainController">

<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <meta name="google-signin-client_id" content="CLIENTID.apps.googleusercontent.com">
</head>

<body>
  <google-sign-in-button button-id="uniqueid" options="options"></google-sign-in-button>

  <script src="https://apis.google.com/js/platform.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.6/angular.min.js"></script>
  <script>
    angular.module('app', [])
      .controller('MainController', ['$scope',
        function($scope) {
          //for more options visit https://developers.google.com/identity/sign-in/web/reference#gapisignin2renderwzxhzdk114idwzxhzdk115_wzxhzdk116optionswzxhzdk117
          $scope.options = {
            'onsuccess': function(response) {
              console.log(response);
            }
          }
        }
      ])
      .directive('googleSignInButton', function() {
        return {
          scope: {
            buttonId: '@',
            options: '&'
          },
          template: '<div></div>',
          link: function(scope, element, attrs) {
            var div = element.find('div')[0];
            div.id = attrs.buttonId;
            gapi.signin2.render(div.id, scope.options()); //render a google button, first argument is an id, second options
          }
        };
      });
  </script>
</body>

</html>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在javascript中,有时显示文字,有时不显示

来自分类Dev

Xamarin WebView有时不显示在UWP中

来自分类Dev

有时在Angular2中不显示图像

来自分类Dev

html有时不显示在电子邮件中

来自分类Dev

有时ImageView中的图像不显示

来自分类Dev

有时不显示波纹效果

来自分类Dev

React 组件有时不显示

来自分类Dev

Google社交登录按钮不显示

来自分类Dev

Volley正在获取数据,但有时它在listview中显示数据,有时不显示

来自分类Dev

在htop的命令列中,有时显示完整路径,有时则不显示。为什么?

来自分类Dev

音量滑块图形有时显示,有时不显示

来自分类Dev

android工具栏有时显示活动名称,有时不显示

来自分类Dev

为什么有时显示背景而有时不显示背景?

来自分类Dev

数据库项目有时不显示在Meteor.js中

来自分类Dev

为什么有时在集合视图单元格中不显示图像视图?

来自分类Dev

如何强制Node.js在登录时不显示我的登录按钮,在注销时不显示我的登录按钮

来自分类Dev

Ember.js有时不显示错误

来自分类Dev

pip:为什么有时不显示 twine 命令

来自分类Dev

PopupMenu有时不显示在屏幕上

来自分类Dev

KnockoutJS foreach有时不显示结果

来自分类Dev

firebase data recyclerview 有时什么都不显示

来自分类Dev

带有AngularJS的Google登录按钮

来自分类Dev

为什么vim有时显示^ M,有时却不显示(即使它们在那里)?

来自分类Dev

为什么显示的图像有时显示而其他时间不显示?

来自分类Dev

LLDB有时显示矢量数据,而其他时候不显示

来自分类Dev

UIImage + SDWebImage不显示某些JPG文件。有时显示JPEG。PNG工作

来自分类Dev

有时我想隐藏DataGridViewButtonColumn中的按钮

来自分类Dev

在选择单选按钮时,div不显示在html中

来自分类Dev

比较两个 json 数组后的正确图像有时不显示

Related 相关文章

  1. 1

    在javascript中,有时显示文字,有时不显示

  2. 2

    Xamarin WebView有时不显示在UWP中

  3. 3

    有时在Angular2中不显示图像

  4. 4

    html有时不显示在电子邮件中

  5. 5

    有时ImageView中的图像不显示

  6. 6

    有时不显示波纹效果

  7. 7

    React 组件有时不显示

  8. 8

    Google社交登录按钮不显示

  9. 9

    Volley正在获取数据,但有时它在listview中显示数据,有时不显示

  10. 10

    在htop的命令列中,有时显示完整路径,有时则不显示。为什么?

  11. 11

    音量滑块图形有时显示,有时不显示

  12. 12

    android工具栏有时显示活动名称,有时不显示

  13. 13

    为什么有时显示背景而有时不显示背景?

  14. 14

    数据库项目有时不显示在Meteor.js中

  15. 15

    为什么有时在集合视图单元格中不显示图像视图?

  16. 16

    如何强制Node.js在登录时不显示我的登录按钮,在注销时不显示我的登录按钮

  17. 17

    Ember.js有时不显示错误

  18. 18

    pip:为什么有时不显示 twine 命令

  19. 19

    PopupMenu有时不显示在屏幕上

  20. 20

    KnockoutJS foreach有时不显示结果

  21. 21

    firebase data recyclerview 有时什么都不显示

  22. 22

    带有AngularJS的Google登录按钮

  23. 23

    为什么vim有时显示^ M,有时却不显示(即使它们在那里)?

  24. 24

    为什么显示的图像有时显示而其他时间不显示?

  25. 25

    LLDB有时显示矢量数据,而其他时候不显示

  26. 26

    UIImage + SDWebImage不显示某些JPG文件。有时显示JPEG。PNG工作

  27. 27

    有时我想隐藏DataGridViewButtonColumn中的按钮

  28. 28

    在选择单选按钮时,div不显示在html中

  29. 29

    比较两个 json 数组后的正确图像有时不显示

热门标签

归档