我如何提供 .config 函数来配置我的库

How can i provide a .config function to configure my library

我为 angular js 制作了一个小代码库。我在我的库的主模块中创建了一个 .config 方法,它依赖于我的 moduleConfigProvider。我希望我的库的使用者在 app.config 阶段(应用程序启动时尽快)在我的配置提供者上调用 .configure 。

问题是我模块中的 .config 似乎 运行 在主应用程序模块的 app.config 之前。我该如何解决这个问题?

例如这就是我使用配置的方式。我需要在 .config 阶段使用它,因为我需要配置 $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);

        }])

这是配置提供程序:

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;
        };

    });

最后是我的 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);
    }])
;

好吧,很简单,只需要在你的提供者函数中使用其他提供者作为依赖即可。

检查以下代码段

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>