Michele Stieven
1 min readApr 4, 2018

--

Simply put, using useClass, the component that injects the provider will get access to the class instance. Similarly, when you use useValue, the component will get the value. When you setup a provider this way:

providers: [ MyService ]

Angular transforms it to:

providers: [ { provide: MyService, useClass: MyService } ]

as to say “I [the framework] will create an instance of MyService for you. Whenever in your components you inject MyService, you’ll get the instance directly, without having to instantiate one on your own”.

There’s one other option which is to use useExisting: this one will use not only the same class but also the same exact instance.
In this case, we need to provide an object, which is a value, and not a class.
Think of “provide:” as something that will be used by the components to get the value, and of “useValue: “ as the value itself.

--

--

No responses yet