1

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?

gumenimeda
  • 815
  • 7
  • 15
  • 43
  • 3
    On the one hand, you seem to be saying that it needs to set before services are instantiated. On the other hand, you seem to be saying that it depends on other services. These are conflicting statements. – Marc Kline Jun 04 '14 at 20:24
  • you are correct, I just figured out what I actually need. getUserIdService and getTokenService should be constants and I should call them within a service 'settings' – gumenimeda Jun 04 '14 at 20:31
  • 1
    Also consider using a Provider instead of a service. It'll be loaded before all services. You could do late dependency injection via this.$get in the provider if you need to. – Erik Duindam Jun 04 '14 at 20:34

1 Answers1

2

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';
});
guru
  • 4,002
  • 1
  • 28
  • 33