Reputation: 835
In my angular application I have a settings module that I made a constant. I figured that it should be a constant because the constants get applied before other provide methods.
I also have 2 services: getUserIdService
and getTokenService
that get me userId
and token
respectively.
angular.module('app').constant('settings', {
streamServer: 'http://someurl.com:9009',
userId: /* $getUserIdService.getUserId() */
token: /* $getTokenService.getToken() */
});
From what I see in the api constant
doesn't have a constructor so I can't pass in any dependencies (getUserIdService
, getTokenService
)
This module needs to be globally available and it doesn't have to be a constant. I just need it to get initialized before any other module.
How can I do this?
Upvotes: 1
Views: 160
Reputation: 4022
Use the angular module run method to initialize your data. You can inject your services into the run method and use it to initialize your data
Sample demo: http://plnkr.co/edit/TW5r7YM5gkevWr2hjmZs?p=preview
JS:
var app = angular.module('plunker', []);
app.value('settings', {
streamServer: 'http://someurl.com:9009',
userId:null ,/* $getUserIdService.getUserId() */
token: null/* $getTokenService.getToken() */
}).run(function($http,$timeout,settings){//inject your services here
settings.userId = 'guru';//call services to get the user id
settings.token = 'tkn39';
});
Upvotes: 2