I read the following recipe that shows how to use the AngularJS interface with the Google Cloud Endpoints backend:
What I don't understand is an application for initializing AngularJS and Cloud Endpoints. The relevant section is as follows:
Appendix: AngularJS + Cloud End # 1 Endpoint Configuration Tips: Be careful with the initialization sequence
The guestbook application loads three different JS libraries in the following sequence:
- Angularjs
- Guestbook Application
- Google API Client Containing Endpoints Features
To follow this sequence, index.html contains the following <script> in the <head> to load each of the JS libraries:
<script src="js/angular.min.js"></script> <script src="js/guestbook.js"></script> <script src="https://apis.google.com/js/client.js?onload=init"></script>
After loading, the third library (Google API Client) calls the initialization function specified by its onload parameter. In this case, the init () function is expected and called. Tip # 2: enter AngularJS World as quickly as possible
In the initialization sequence, we use two functions:
init() function window.init() function
This init () function is defined in the .js guestbook as follows:
function init() { window.init(); }
As you can see the above code, the function simply calls the window.init () function (i.e. the init () function defined in the global window object) and does nothing. Window.init () is defined in an AngularJS controller as follows:
$window.init= function() { $scope.$apply($scope.load_guestbook_lib); };
In AngularJS, the global window object is referenced by the "$ window" notation, which is a wrapper for it. It is best practice in AngularJS not to access the window object directly in order to improve testability.
The reason you don't want to initialize the first init () method is because you can put as much code as possible in the AngularJS world, such as controllers, services, and directives. As a result, you can use all the power of AngularJS and all your unit tests, integration tests, etc.