Angular Js and google api client.js (gapi)

后端 未结 8 1515
不思量自难忘°
不思量自难忘° 2020-11-29 00:46

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 a

8条回答
  •  爱一瞬间的悲伤
    2020-11-29 01:16

    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)

    
    
      
      
      
    
    
    

    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.

提交回复
热议问题