在 angular 配置中同时注入一个服务和提供者

Inject at same time a service and provider in angular configuration

我有一个 angular 配置:

(function () {
    'use strict';

    angular
        .module("app.functionality")
        .config(config);

    //  +------------------------------------------------------------------+
    //  |   Implementations                                                |
    //  +------------------------------------------------------------------+
    config.$inject = ["$stateProvider"];

    function config($stateProvider) {
        $stateProvider.state("aState", {
                url: "/:idf/:idc",
                views: {...},
                resolve: {
                    aPromise: function ($stateParams) {
                        return {
                            idf: $stateParams.idf,
                        };
                    }
                },
            });
    }
})();

和其他地方:

(function () {
    'use strict';

    angular
        .module("MyService", [])
        .factory("myService", service);

    service.$inject = ["$http"];

    function service($http)
    {
        // implementations
    }
})();

这很好用,我可以获得 idf 值。我真正想要的是:

这组更丰富的数据应该通过服务检索(紧接在 idf 属性 之后)

richerSetOfData: aService.getSomething($stateParams.idf)

但是我知道我无法将服务注入 config(只是提供者)。我可以使用 run,但 运行 不太适合提供者(我需要 $stateProvider)。那么我怎样才能做我需要的呢?最佳做法是什么?

你可以在解析函数中注入服务:

       resolve: {
            aPromise: ['$stateParams', 'myService', function ($stateParams, myService) {
                return {
                    idf: myService.doSmthng($stateParams.idf)
                };
            }]
        },