Generally, Angular gives you the module.run(fn) API to do initializations. The fn argument is fully injectable, so if you have a service, e.g. myService, that exposes an init() method, you can initialize it as:
angular.module(...).run(['myService', function(myService) {
myService.init();
}]);
If the service initialization code is placed in the service constructor, i.e.:
angular.module(...).service(function() {
...initialization code...
});
...then it is enough to just declare the dependency on the service in your run() method, i.e.:
angular.module(...).run(['myService', function() {
// nothing else needed; the `myService` constructor, containing the
// initialization code, will already have run at this point
}]);
In fact, you can abbreviate it as below:
angular.module(...).run(['myService', angular.noop]);
(Sidenote: I find this pattern a bit awkward; if a service contains only initialization code, just implement the initialization code directly in a run() function. You can attach mulitple run() functions to each module anyway.)
module.run()for initializations; if you want to encapsulate the logic in a service, haverun()depend on that service and invoke aninit()function (or just use the constructor as you did).run()dependent on my service. I just gave it a function pointer that had a$injectproperty.