The ability to reply to discussions is limited to PRO members. Want to join in the discussion? Click here to subscribe now.

Understanding Providers

Understanding Providers

3:57
In this lesson, we’re going to take a look at how add a class to the providers property of a component creates an actual providers. We’ll learn what a provider specifically does and how we can provide different dependencies with the same token.
Watch this lesson now
Avatar
egghead.io

In this lesson, we’re going to take a look at how add a class to the providers property of a component creates an actual providers. We’ll learn what a provider specifically does and how we can provide different dependencies with the same token.

Avatar
Michael

I am a bit surprised to see that the component is responsible for saying which implementation should be used for a given service type (provider in Anglular2). This configuration is typically done outside of the component itself when using DI container. e.g. the component would specify that a DataService is required but not which implementation to use.

  • What's your opinion on that?
  • How can this configuration outside of the component be achieved in Angular2?

Cheers, Michael

Avatar
Raphi

I am a bit surprised to see that the component is responsible for saying which implementation should be used for a given service type (provider in Anglular2). This configuration is typically done outside of the component itself when using DI container. e.g. the component would specify that a DataService is required but not which implementation to use.

  • What's your opinion on that?
  • How can this configuration outside of the component be achieved in Angular2?

Cheers, Michael

I think the answer is that you can create your own custom providers which make decisions about which service to provide. Then you can pass "DataServiceProvider" to the providers array of the component and have DataServiceProvider determine which service to provide. This moves the decision making out of the component

EDIT:
Your answer is in the next video :)

In reply to Michael
Avatar
Elliot

It's still a little scary to me how with TypeScript, you can make 2 different classes and have them share the same public interface - then not define an interface for them to explicitly extend, but then still use the 2 classes interchangeably. I would've thought that the TypeScript compiler would complain about them not explicitly extending an interface if you've annotated the type explicitly like that.

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?