AngularJS and the endpoint of the Google Cloud: go through the necessary

I am new to AngularJS, but I really like how AngularJS works, so I want to deploy it as a client part for my Google endpoint backend. Then I immediately get two problems:

1, where to put myCallback so that it can work in the ANgularJs controller?

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

2, How can I do oauth2? and how does the controller know if the user is allowed?

 gapi.auth.authorize({client_id: myCLIENT_ID, scope: mySCOPES,..... 

Any help is appreciated.

+4
source share
3 answers

To load the Javascript JavaScript library using AngularJs, the callback function passed to onLoad from the Javascript Library is a function that loads AngularJS, for example:

This refers to the ending of the html file:

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

Then, in the <head> section, you load angular as follows:

 <script type='text/javascript'> function startApp() { var ROOT = 'http://<yourapi>.appspot.com/_ah/api'; gapi.client.load('myapifromgoogleendpoint', 'version1', function() { angular.bootstrap(document, ["myModule"]); }, ROOT); } </script> 

As described by Kenji, you also need to remove the ng-app directive from your html.

+5
source

As for the callback - to access the Angular controller you need to use an injector ( http://docs.angularjs.org/api/AUTO . $ Injector)

Just create a global callback function, and then get the controller link as follows:

 window.callbackFunction() { injector = angular.element(document.getElementById('YourController')).injector() injector.invoke(function ($rootScope, $compile, $document) { $rootScope.variable = "stuff you want to inject"; }) } 

In this example, I am inserting data into rootScope, but this will also work for a specific area of ​​the controller (just enter $ scope instead)

It can't help with the second question, since I'm not familiar with gapi, although making auth2 calls from angularjs is pretty straight forward.

0
source

Here you will find detailed information on how to use angularjs with google endpoints:

https://cloud.google.com/developers/articles/angularjs-cloud-endpoints-recipe-for-building-modern-web-applications?hl=es

0
source

All Articles