I got error with \"No provider for ObservableDataService\"
ObservableDataService
:
https://github.com/sanex3339/bannerscreator/blob/master/resources/assets/t
IN fact when you implement a service you don't need to decorate it using the @Component
decorator. Using the @Injectable
decorator is enough.
I guess that what is a bit disturbing is that there is no way to set providers on the @Injectable
decorator. The reason for this is that providers are linked to components. All the processing executed from a component will use the injector of the component.
I mean if componentA calls serviceA that calls serviceB. The @Injectable
decorator will try to resolve serviceB in the componentA injector.
Another part that is important is the "hierarchical injectors" feature of Angular2. Here is an overview of all these elements and their relations with my sample:
Application
(application injector)
|
ComponentA --- ServiceA --- ServiceB
(component injector)
In such application, we have two injectors:
bootstrap
functionAppComponent
injector that can be configured using the providers
attribute of this component. It can "see" elements defined in the application injector. This means if a provider isn't found in this provider, it will be automatically look for into this parent injector. If not found in the latter, a "provider not found" error will be thrown.So in your case, you can define ObservableDataService
within providers for the component or above. Sure defining it within bootstrap is the widest...
This question could give you more details about how hierarchical injectors of Angular work: