1

What would be the right (angular-wise) way to load data in the app.config block?

Would a boot module+provider help? i tried the following approach (streamlined) http://jsfiddle.net/Vd5Pg/1/ with no success.

angular.module('boot',[]).provider('test', function(){

  this.$get = function() {
    return {
      getString: function() { return "i am a string"; }
    }
  };

});

angular.module('main',['boot']).config(['testProvider', function(test) {

  //no luck with getString


}]);

The real life situation is that i'd like to load localized routes and determine current language before configuring the $routeProvider. TIA.

jurito
  • 351
  • 3
  • 16

1 Answers1

0

Your current code returns a test service with the method getString. In order to getString() within the provider you should do the below.

angular.module('boot',[]).provider('test', function(){

  // Method available in testProvider
  this.getString = function () {
     return "i am a string";
  }

  this.$get = function() {
    return {
      // Your test service.
    }
  };

});

angular.module('main',['boot']).config(['testProvider', function(test) {

  var myString = testProvider.getString();


}]);
eddiec
  • 7,608
  • 5
  • 34
  • 36
  • well thanks, now i understood how providers work :). I still am stuck to my problem though, not being able to inject $http my provider. How should i load data so that's available in my config block and feeds my $routeProvider? – jurito Oct 25 '13 at 08:29
  • you can't inject $http during config, it runs before those services are loaded. I see you have two options. 1) Load the correct configuration during your build process (are you using grunt?), 2) Use an approach like this http://stackoverflow.com/questions/13681116/angularjs-dynamic-routing – eddiec Oct 25 '13 at 08:39
  • 1
    beautiful! i needed what eazel7 explains in the last answer. thanks A LOT. – jurito Oct 25 '13 at 09:33