Angular Js and google api client.js (gapi)

24,062

Solution 1

Rather than bootstrapping or setting a timeout, it's most efficient to let Angular load before/while you're making the server requests. I followed the advice described in AngularJS + Cloud Endpoints: A Recipe for Building Modern Web Applications, which does the following.

Keep your ng-app directive as usual (no bootstrapping)

<html ng-app="myApp">
<head>
  <script src="angular.js" type="text/javascript"></script>
  <script src="app.js" type="text/javascript"></script>
  <script src="https://apis.google.com/js/client.js?onload=init"></script>
</head>
<body ng-show="backendReady">

Create a global variable for the GAPI callback function anywhere in your JS

var app = angular.module('myApp', []);

var init = function() {
  window.initGapi();
}

app.controller('MainController', function($scope, $window, gapiService) {
  var postInitiation = function() {
    // load all your assets
  }
  $window.initGapi = function() {
    gapiService.initGapi(postInitiation);
  }
});

app.service('gapiService', function() {
  this.initGapi = function(postInitiation) {
    gapi.client.load('helloWorld', 'v1', postInitiation, restURL);
  }
});

From link above:

The reason why you would not want to execute the initialization in the first init() method is so you can put as much of the code as possible in the AngularJS world, such as controllers, services and directives. As a result, you can harness the full power of AngularJS and have all your unit tests, integrations tests,and so forth.

This may seem like a roundabout way of doing things, but it optimizes for speed, testability, and separation of concerns.

Solution 2

Nice post and thanks! This approach worked for me. It might matter what order that the code appears in your index.html file. It did not work for me until I had things inthis order.

...
<script>
  function googleOnLoadCallback(){
      alert('googleOnLoadCallback called');
      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, ["myApp"]);
              console.log("myApp bootstrap complete " + gapi);
          };
      };
      gapi.client.setApiKey("my_client_id");
      gapi.client.load("translate", "v2", gCallback);

  }
</script>
<!-- See https://developers.google.com/api-client-library/javascript/samples/samples -->
<script src="https://apis.google.com/js/client.js?onload=googleOnLoadCallback"></script>
</head>

Solution 3

Although pretty much on progress maybe also worth to mention angular-googleapi, which wraps nicely some Google Calendar and Google Plus API calls and easy extendable.

You'd need to add this bit to your controller when checking for authorisation:

$scope.authenticated = false;

$scope.$on("google:authenticated", function(){
   $scope.authenticated = true;
   $scope.$on('googleCalendar:loaded', function(){
       # work your magic here
       # $scope.calendars = googleCalendar.listCalendars();
       # $scope.$apply();
   });
});

function checkAuth() {
   setTimeout(function(){
       gapi.auth === undefined ? checkAuth() : googleLogin.checkAuth();
   }, 20);
}

checkAuth();

Solution 4

I wrote a simple directive to load the google map API asynchronously :

// js/directives/gmapAsync.js

(function(){
'use strict';

angular.module('app').directive('gmapAsync',
    ['$window', '$rootScope', gmapAsync]
);

function gmapAsync($window, $rootScope){

    var gmapScript = $window.document.createElement('script');  

    $window.onGmapScriptLoaded = function(){
        console.log('google maps script loaded');

        $rootScope.gmapApiLoaded = true;
        $rootScope.$broadcast('gmap.api.loaded');

    };

    return {
        restrict: 'A',
        transclude: false,
        scope:false,
        link:   function(scope, element, attributes){

            if (navigator.onLine) {
                appendScript();
            } else {
                $window.addEventListener('online',appendScript);
            }

            function appendScript(){
                gmapScript.type = 'text/javascript';
                gmapScript.src = 'https://maps.googleapis.com/maps/api/js?v=3.exp&' + 'callback=onGmapScriptLoaded';
                $window.document.body.appendChild(gmapScript);
            }
        }
    };
}
})();

Then in your main controller, you can handle the event :

// js/controllers/AppCtrl.js

(function(){
'use strict';

    angular.module('app').controller('AppCtrl',[$scope,AppCtrl])

    function AppCtrl($scope){

        $scope.$on('gmap.api.loaded',function(){
            // your stuff to init after the api is loaded
        });
    }

})();

You just have to declare the directive in your body tag :

<!DOCTYPE html>
<html>

    <head></head>

    <body data-ng-app="app" data-gmap-async data-ng-controller="AppCtrl">

        <!-- template body -->

        <script type="text/javascript" src="js/app.js"></script>
        <script type="text/javascript" src="js/controllers/AppCtrl.js"></script>
        <script type="text/javascript" src="js/directives/gmapAsync.js"></script>
    </body>

</html>
Share:
24,062
Samuel
Author by

Samuel

Updated on August 17, 2020

Comments

  • Samuel
    Samuel almost 4 years

    It took me one day to make it works so I think my experience may be useful from someone. And maybe some others will find improvement.

    So I start angularJS two days ago. And I want it works with Google Cloud Endpoints to create a backend interface. Here comes the trouble for me.

    The javascript client for gapi comes with asynchronous loading, so angular initialization will crash having gapi undefined.

    So you need to bootstrap angular when gapi is initialized:

    1. remove ng-app="myApp"
    2. Add <script src="https://apis.google.com/js/client.js?onload=googleOnLoadCallback"></script>
    3. Add the callback:

      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, ['myApp']);  
                  console.log('Angular bootstrap complete ' + gapi);  
              };  
          };  
          gapi.client.load('helloWorld', 'v1', gCallback, '//' + window.location.host + '/_ah/api');  
      }
      

    Feel good but how about a call ?

    So here is the controller:

    angular.module('myApp.controllers', []).  
        .controller('MyCtrl', ['$scope' ,'helloWorldService',  
            function($scope,greetingsService) {
              helloWorldService.loadData($scope);  
        }]);
    

    And here is the service:

    angular.module('myApp.services', [])
    service('helloWorldService', [function() {
       this.loadData = function($scope)  {
         //Async call to google service
         gapi.client.helloWorld.greetings.listGreeting().execute(
            function(resp) {
                if (!resp.code) {
                    console.debug(resp);
                    $scope.greetings = resp.items;
                    // Because it's a callback,
                    // we need to notify angular of the data refresh...
                    $scope.$apply();
                }
          });
       };
    }]);
    

    And magically your page updates thanks to angular.

    Feel free to mark anywhere I go wrong.

  • willlma
    willlma over 9 years
    See my answer on how to avoid setting a timer.
  • willlma
    willlma about 9 years
    @CadeThacker Your edit was rejected, but it was correct, so I replicated it. Thanks.
  • mpgn
    mpgn about 9 years
    what about infinit loop stackoverflow.com/questions/28732667/… ?
  • willlma
    willlma about 9 years
    See my answer there. TLDR: use different function names for init and initGapi
  • contributorpw
    contributorpw about 9 years
    Not sure that I fully understand. But why we can't append checkAuth() to the provider? It seems to be more comfortable. Thank you for your advice.
  • Tianxiang Zhang
    Tianxiang Zhang almost 9 years
    Hi Willlma, I am trying to use this method. But i met an error saying window.initGapi is not defined. I define the method in a BaseControler which tied to the Body.
  • willlma
    willlma almost 9 years
    Hmm, but site why unit would die before the controller. Make a question with your code and I'll have a look.
  • Tianxiang Zhang
    Tianxiang Zhang almost 9 years
    I have created a new question for this. stackoverflow.com/questions/31524300/…
  • fuelusumar
    fuelusumar almost 8 years
    if i could give you more upvotes i would, excellent answer and helped me a lot. I had hours trying to get this working on my own until i saw this, thanks.