Typically, Angular provides an API module.run(fn)for initialization. The argument is fnfully injectable, so if you have a service, for example myService, that provides a method init(), you can initialize it as:
angular.module(...).run(['myService', function(myService) {
myService.init();
}]);
If the service initialization code is placed in the service constructor, that is:
angular.module(...).service(function() {
...initialization code...
});
... then just declare the dependency on the service in your method run(), that is:
angular.module(...).run(['myService', function() {
}]);
In fact, you can shorten it as shown below:
angular.module(...).run(['myService', angular.noop]);
(Sidenote: I find this template a bit inconvenient, if the service contains only the initialization code, just implement the initialization code directly in the function run(). In any case, you can attach the mulitple functions run()to each module.)
source
share