I have a common service which serves configuration details from backend that I am using in other services...is there a way to load the common service before loading other services. Since I am getting an exception in JS "cannot read property "url" of null". Sometimes It works fine if the configuration loaded successfully before other calls
Asked
Active
Viewed 336 times
3 Answers
0
It sounds like using module.run()
would help solve your problem as you can initialise your common service:
var app = angular
.module('demoApp', [])
.run(['commonService', function(commonService) {
commonService.Init()
}]);

Brian Giggle
- 26
- 5
-
I tired this but i am getting Error: ng:areq Bad Argument – Tikigouri Jun 23 '16 at 15:37
-
Are you calling `.run()` on your module? – Brian Giggle Jun 23 '16 at 15:43
-
yes i am calling in .run(function($rootScope,commonService) { commonService.initialize(); }); – Tikigouri Jun 23 '16 at 18:12
-
HI if you are still having issues you should post some of your code so we can take a look and help – Brian Giggle Jun 24 '16 at 12:35
-
angular.module("app.services") .run(function( $location,commonService) { commonService.initialize(); }) //This is the config: .config(function($httpProvider, jwtInterceptorProvider) { jwtInterceptorProvider.tokenGetter = ['configDetail', 'commonService' , function(configDetail, commonService) { var appConfigObj = commonService.getUrl(); }]; $httpProvider.interceptors.push('jwtInterceptor'); }); – Tikigouri Jun 24 '16 at 17:11
0
Few suggestions,
- You can make use module.run function which will execute immediately after module.config function, make AJAX/HTTP call their and keep it in $rootScope/Service, or as angular constant.
- Interceptors: you can also make use of the $http's interceptors, so when ever you makes an AJAX request we can configure to execute the interceptor first, so that we can check whether the configs loaded or not, if not then we can grab that first.
- If you are using angular routing, then we can make use of resolve convention.

akhil.cs
- 691
- 1
- 5
- 12
-
I am new to angularJs..if you can provide me some sample code...that would be great...Thanks for replying – Tikigouri Jun 23 '16 at 15:36
0
You can set up a service deferred, which holds a promise that gets resolved after initialization of the service is complete. Then when asking for a value of the service return a promise chained to the deferred promise:
angular.module('YourApp').factory("CommonService", function(){
var initDeferred = $q.defer();
var initialize = function() {
/* initialize your service, maybe do something async, maybe set local variables with obtained info
* then resolve deferred promise (maybe with obtained values)*/
initDeferred.resolve(/* initialized service data */);
};
var getUrl = function() {
return initDeferred.promise.then(
function(initializedServiceData) {
return initializedServiceData.url;
}
);
};
return {
initialize: initialize,
getUrl: getUrl
}
});
And don't forget to initialize the service. A good point to do so is the run() function of the module as Brian Giggle already suggested.
angular.module('YourApp', []).run(function(CommonService){
CommonService.initialize();
});
You are able then to retrieve the initialized data as a promise:
angular.module('YourApp').controller('YourController', function($scope, CommonService){
CommonService.getUrl()
.then(function(url){
$scope.url = url;
})
});

Simon Z.
- 497
- 4
- 11
-
I am initializing the commonService in the run..but i am getting Uncaught TypeError: configService.initialize is not a function this error and i am getting this error as well initDeferred.then is not a function... – Tikigouri Jun 23 '16 at 17:03
-
sorry, there was an error in the code, just replace `return initDeferred.then(...)` with `return initDeferred.promise.then(...)` – Simon Z. Jun 23 '16 at 17:09
-
as for the `configService.initialize is not a function` error. You do `var initialize = function(){/*...*/};` and `return {initialize: initialize}` in the "configService", right? – Simon Z. Jun 23 '16 at 17:16
-
Thank you so much...it is working fine...i have last question..i want to use url value in another service from commonService where i don't want to make the service call again..I am injection commonService in other services based on the need..but i don't want to make the ajax call again if value is there – Tikigouri Jun 23 '16 at 19:41
-
services in angularjs are [singletons](https://docs.angularjs.org/guide/services), you don't have to `initialize()` again after injecting the service in another service, the data is stored in the service singleton. So if you call `configService.getUrl()` in another service you will also receive a promise which will be immediately resolved with the url (without any AJAX call) – Simon Z. Jun 23 '16 at 20:20
-
I am facing another issue with commonService..i am getting undefined for apiUrls when i am calling commonService to get urls in config...I think config module executes before run module. should i initialize common service inside the config module – Tikigouri Jun 23 '16 at 23:23
-
I'm not sure if I understood you right: do you mean a config module (you have something like `angular.module("Config", [])`) or do you mean the config block OF a module (you have something like `angular.module("SomeApp", []).config(function(){/* config block logic */})`)? If its the config block then yes - the config block runs before the run block and if you want to be able to get the `configService` in the config block you need to define it with the [Provider Recipe](https://docs.angularjs.org/guide/providers). Note: The $http service can be made available in config phase only with a... – Simon Z. Jun 24 '16 at 12:32
-
...[trick to provide $http service in config phase](http://stackoverflow.com/questions/17497006/use-http-inside-custom-provider-in-app-config-angular-js) – Simon Z. Jun 24 '16 at 12:33