2

I created an oauth2 service to use a Google API. The following line loads the JS Client library and registers an onload handler which fires when the library has finished loading:

<script src="https://apis.google.com/js/client.js?onload=load"></script>

My angular app is defined:

var myapp = angular.module('myapp', ['ui.state', 'ui.bootstrap', '$strap.directives']);

and the service

myapp.factory('oauth2', function($rootScope) {
    this.load = function() {
        console.log("Load...");
        gapi.client.load('oauth2', 'v2', function() {
            console.log("OAuth2 API loaded");
        });
    };

    ...

    return this;
});

The load callback is not called. I also tried oauth2.load, myapp.load but none of these work. How do you configure the onload, to call oauth2.load?

Sydney
  • 11,964
  • 19
  • 90
  • 142
  • Very curious about this. I have only tried oAuth in pure JS, not within angular. Perhaps since the callback is itself outside of angular, one of [these methods](http://stackoverflow.com/questions/10490570/call-angular-js-from-legacy-code) is necessary?? – rGil Jul 03 '13 at 21:13

1 Answers1

2

This is a complete guess, and may not even be close, but perhaps it can spurn others to take a look.

My guess involves accessing the angular service from outside angular. I'm using this post as a reference:

// since the callback is referencing the function 'load', define it here:

var load = function(){
    var elem = angular.element(document.querySelector('[ng-controller]'));
    var injector = elem.injector();
    var oauth2 = injector.get('oauth2') // grab the factory
    return oauth2
}

Again - just a guess. If it doesn't work, comment below and I'll delete!!

Community
  • 1
  • 1
rGil
  • 3,719
  • 1
  • 22
  • 30