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.

1 Answer 1

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();


}]);
Sign up to request clarification or add additional context in comments.

3 Comments

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?
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 stackoverflow.com/questions/13681116/angularjs-dynamic-routing
beautiful! i needed what eazel7 explains in the last answer. thanks A LOT.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.