I'm in the middle of refactoring my ngTrader game by following the angularjs-google-style to have all my controllers, services, directives, ect... defined as classes, with functions defined as prototypes on those classes, then registering the class with angular.
The issue is getting these changes it to work with closure-library's goog.provide()
and goog.require()
. With the setup below, I get an angular error that says
Failed to instantiate module ngTrader due to:
Error: [$injector:modulerr] Failed to instantiate module ngTrader.account due to:
Error: [ng:areq] Argument 'fn' is not a function, got undefined
Index.html
<body ng-app="ngTrader">
....
<script src="bower_components/closure-library/closure/goog/base.js"></script>
<script src="components/account/accountSrvc.js"></script>
<script src="components/account/account.js"></script>
<script src="app.js"></script>
</body>
I've tried various orders of these files, thinking that the load order will affect what is available and what is not. I figured that the order shouldn't matter since that's what goog provide/require should fix.
- app.js, account.js, accountSrvc.js
- app.js, accountSrvc.js, account.js
- accountSrvc.js, account.js, app.js
app.js
goog.provide('ngTrader');
goog.require('ngTrader.account');
ngTrader = angular.module('ngTrader', [ngTrader.account.name]);
account.js
goog.provide('ngTrader.account');
goog.require('ngTrader.account.accountSrvc');
ngTrader.account = angular.module('ngTrader.account', []);
ngTrader.account.service('accountSrvc', ngTrader.account.accountSrvc);
Not sure if I need the goog.require()
call here, the google style didn't include it their snippet. I've tried with and without it. I've also tried adding goog.require('ngTrader')
here, but that would throw errors unless I moved app.js to be the first called script in index.html, which in turn would throw errors about ngTrader.account not being defined.
The service registration is done here as per the google style recommendation services example shows to put this module.service('request', hello.request.Request);
in the module definition.
accountSrvc.js
goog.provide('ngTrader.account.accountSrvc');
ngTrader.account.accountSrvc = function() {....};
ngTrader.account.accountSrvc.prototype.reset = function() {....};
I put breakpoints inside accountSrvc.js and see that the ngTrader.account object has the new accountSrvc property being added to it. But when I inspect breakpoints in account.js, the accountSrvc property is undefined.
So where am I going wrong with this, order of the scripts being loaded, how I'm using require/provide, or something else?
Any help would be appreciated. Thanks.