Components: Can you move HttpModule in MdIconModule to component scope (or don't import it)?

Created on 12 Jan 2017  路  8Comments  路  Source: angular/components

Bug, feature request, or proposal:

Bug

What is the current behavior?

The feature modules that import MaterialModule (import MdIconModule) will create separated http provider and override the application-wide customized Http.

What are the steps to reproduce?

The following link shows the problem.
http://plnkr.co/edit/hDLOuvRPWxPIY3TwX4GB?p=preview

What is the use-case or motivation for changing an existing behavior?

Cannot use application-wide customized Http in feature module that import MaterialModule

Which versions of Angular, Material, OS, browsers are affected?

@[email protected]
@angular/[email protected]
Google Chrome Version 57.0.2970.0 (Official Build) dev (64-bit)
Window 10

P3 has pr help wanted

All 8 comments

@alxhub do you have any input on how a component that depends on Http should depend on HttpModule WRT this issue?

I'm facing the same issue. I created a custom http service:

@Injectable() export class SecureHttp extends Http { ... }

I provide this service in app.module.ts:

providers: [ { provide: Http, useClass: SecureHttp } ],

If I don't use Material Design, this SecureHttp service is used in all classes that inject the Http service. But if I do include MaterialModule in other modules as well, the default Http service is used instead of my SecureHttp, because the HttpModule is used in MdIconModule. I can fix this by adding
providers: [ { provide: Http, useClass: SecureHttp } ],
in each module.

Is this intended behavior?

Had some discussion w/ @alxhub about this. The idea is to change md-icon to optionally inject Http and throw an error if it isn't provided without directly importing the HttpModule itself.

HI @jelbourn , @EladBezalel

Upon your last comment, I thought about this logic.
remove of direct importing of HttpModule and in icon-registry get the Injector and check
if there already Http implementation supplied, if so use it or use the ReflectiveInjector to create the default one.

  private getHttp() {
    let httpService: Http = this._injector.get(Http ,null);

    if (!httpService) {
      let providers = (<any>HttpModule).decorators[0].args[0].providers;
      let injector = ReflectiveInjector.resolveAndCreate(providers);
      httpService = injector.get(Http);
    }

    return httpService;
  }
}

Done in #3792

If I install @angular/material and uninstall @angular/http in a fresh cli project, ng serve wont compile. Is this by design? I expected that the http package did not need to be installed due to it being removed from peerDependencies in 3792.

(I get similar errors with @angular/animations. Only core/common packages are in peerDependencies, even though TS/Webpack require http and animations too).

@csvn please file a new issue

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

_This action has been performed automatically by a bot._

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Hiblton picture Hiblton  路  3Comments

crutchcorn picture crutchcorn  路  3Comments

alanpurple picture alanpurple  路  3Comments

jelbourn picture jelbourn  路  3Comments

3mp3ri0r picture 3mp3ri0r  路  3Comments