Search code examples
angularjsangular-providers

How to inject multiple providers in app.config in Angularjs


I have multiple providers in my project and each provider have its own constructor,I am trying to inject all these providers into main app.config. I tried bellow scenario but its not working

tried to add all providers inside the array itself but its not working

app.config([
'EquityValueProvider', function (EquityValueProvider) {
EquityValueProvider.setAPIURL('https://localhost:44333/api/equityvalue');
},
'HeatMapServiceProvider', function (HeatMapServiceProvider) {
HeatMapServiceProvider.setAPIURL('https://localhost:44333/api/equityvalue');
},
'RetailerProvider', function (RetailerProvider) {
RetailerProvider.setAPIURL('https://localhost:44333/api/equityvalue');
}]);

but the bellow code is working (multiple config)

 app.config([
'EquityValueProvider', function (EquityValueProvider) {
EquityValueProvider.setAPIURL('https://localhost:44333/api/equityvalue');
 }])
.config([
'HeatMapServiceProvider', function (HeatMapServiceProvider) {
HeatMapServiceProvider.setAPIURL('https://localhost:44333/api/equityvalue');
}])
.config([
'RetailerProvider', function (RetailerProvider) {
RetailerProvider.setAPIURL('https://localhost:44333/api/equityvalue');
}]);

But here i am adding multiple config,instead of that can i make a single config and add all the providers.


Solution

  • Provide three arguments to the config function:

    app.config([
        'EquityValueProvider','HeatMapServiceProvider','RetailerProvider',   
        function (EquityValueProvider,HeatMapServiceProvider,RetailerProvider) {       
            EquityValueProvider.setAPIURL('https://localhost:44333/api/equityvalue');
            HeatMapServiceProvider.setAPIURL('https://localhost:44333/api/equityvalue');
            RetailerProvider.setAPIURL('https://localhost:44333/api/equityvalue');
        }
    ]);
    

    For more information, see AngularJS Developer Guide - Dependency Injection.