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?

1
  • 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 is necessary?? Commented Jul 3, 2013 at 21:13

1 Answer 1

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!!

Sign up to request clarification or add additional context in comments.

2 Comments

Works like a charm, I just query for ng-app instead of ng-controller
Good to know. I'll be trying this out now, too :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.