How to inject multiple providers in app.config in Angularjs

403 views Asked by At

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.

2

There are 2 answers

0
georgeawg On BEST ANSWER

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.

0
Srdjan On

Here is how:

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');
}]);

You can add as many providers as you want.