0

I have made a little library of code for angular js. I have created a .config method in my library's main module that depends on my moduleConfigProvider. I expect the consumer of my library to call .configure on my config provider during the app.config stage (asap on app start).

The problem is that my .config in my module seems to run BEFORE the app.config of the main app module. How can I work around this problem?

e.g. This is how i'm consuming the config. I need to consume it during the .config stage because I need to configure things like $httpProvider.

// this module provides initial configuration data for module
angular.module('mymodule')
    .config(['$httpProvider', 'someOptionsProvider', 'myConfigProvider',
        function ($httpProvider, someOptionsProvider, myConfigProvider) {

            console.log("Consuming config now: ", myConfigProvider.config);

        }])

Here is the config provider:

angular.module('mymodule.config')
  .provider('myConfig', function() {

        var _this = this;

        _this.configure = configureMethod;
        _this.config = {};

        function configureMethod(configData) {
          _this.config = configData;
            console.log("Config set to:", configData);
        };


        this.$get = function() {
            return _this;
        };

    });

And finally here is my app.config:

angular.module('app')
.config(['myConfigProvider', function(myConfigProvider) {

        console.log("Running App config:", myConfigProvider);

        var config = { a: 1 };

        console.log("Config ready to send:", config);
        myConfigProvider.configure(config);
    }])
;
user230910
  • 2,353
  • 2
  • 28
  • 50
  • Maybe something you need https://stackoverflow.com/a/26408640/3085279 – Tan Duong Apr 19 '18 at 04:07
  • 1
    do you want your library's config block to run after the main module's config block which is using using your library ?? – Shridhar Sharma Apr 19 '18 at 04:11
  • yes, i want my library's `.config()` to happen after the main module `.config()` – user230910 Apr 19 '18 at 04:13
  • @TanDuong thanks for the link, but it doesn't seem to be applicable? – user230910 Apr 19 '18 at 04:15
  • @ShridharSharma - or, if that isn't possible, i would like to know how i should change my design., so that it works correctly – user230910 Apr 19 '18 at 04:15
  • but first tell me why are you willing to running your libraries config block after main main module's config block. – Shridhar Sharma Apr 19 '18 at 04:25
  • @ShridharSharma - I need config from the user of my library to know how to configure $httpProvider and others during _their_ config stages, so I am forced to do the work during my library's .config stage. The main application .config sounds like the right place to tell my library the config data.. – user230910 Apr 19 '18 at 04:28

1 Answers1

1

Okay, it is simple, just use other providers in your provider function as dependencies.

Check the following snippet

angular.module('mymodule', [])

  .provider('myConfig', [
    '$httpProvider',
    function($httpProvider) {

      var _this = this;

      _this.configure = configureMethod;
      _this.config = {};

      function configureMethod(configData) {
        //Do here anything with the $httpProvider
        _this.config = configData;
        console.log("Config set to:", configData);
        console.log('Configuring $httpProvider')
      };


      this.$get = function() {
        return _this;
      };

    }
  ])

angular.module('app', ['mymodule'])
  .config(['myConfigProvider', function(myConfigProvider) {

    console.log("Running App config:");

    var config = {
      a: 1
    };

    console.log("Config ready to send:", config);

    myConfigProvider.configure(config);
  }]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app">

</div>
Shridhar Sharma
  • 2,337
  • 1
  • 9
  • 13