1

My angular 2.4.9 application A uses Http from @angular/http and another (3rd party) Angular Module B which itself also uses Http.

A -> http
A -> B -> http 

In order to add generic HTTP headers, I've created a CustomHttp class in A that extends Http. I've injected it inside my app.module.ts like so

import AppComponent from './app.component';
import { Http, XHRBackend, RequestOptions } from '@angular/http';
import { CustomHttp } from '../myhttp/custom.http';
import { B } from 'some/thirdparty/module';             


@NgModule({
  bootstrap: [
    AppComponent
  ],
  declarations: [
    AppComponent            
  ],
  imports: [
    B.forRoot({ ... /* custom config */ })           
  ],
  providers: [{
      provide: Http,
      deps: [XHRBackend, RequestOptions],
      useFactory: (backend: XHRBackend, defaultOptions: RequestOptions) => {
        return new CustomHttp(backend, defaultOptions);}
    }]
})

This works for all usages of Http inside my application module A. However, it does not work for the B dependency. All usages of Http inside B still seem to use the original class.

I know that DI in Angular works hierarchically https://angular.io/docs/ts/latest/guide/hierarchical-dependency-injection.html.

Is there a way to tell Angular also to inject CustomHttp in the usages of Http in B?

Robert
  • 1,710
  • 2
  • 18
  • 35
  • Please create plunker – yurzui Mar 17 '17 at 15:56
  • There is a similar question with my plunker http://stackoverflow.com/questions/40873480/how-to-provide-custom-provider-to-the-all-lazy-loaded-modules#comment72260686_40873480 But maybe they are completely different cases – yurzui Mar 17 '17 at 16:01
  • I've assembled a plunker: https://plnkr.co/vGAlJA43OKthDFuyHpsa (checkout the console). That structurally reflects what I have in my code base. Unfortunately in this example everything works as expected. I have to find out why. But maybe this already helps others. – Robert Mar 17 '17 at 17:27
  • Okay, meanwhile I realized that the configuration above actually works - I had other issues that were not related to the config above. All instances of Http in all sub modules inside A and in B have been replaced by CustomHttp. – Robert Mar 21 '17 at 16:25

0 Answers0