A
A
Abc Edc2016-04-03 14:59:05
JavaScript
Abc Edc, 2016-04-03 14:59:05

Service inject into service in angular 2?

Do I understand correctly that if in the component (RandomCpm) I inject service A, which in turn injects service B, and maybe it also injects something (C, D.etc), then in the providers of such a RandomCpm component I must specify everything these services(A,B,C,D...) and not just service A? and a small sub-question about dependencies, if I do not explicitly specify providers in the SubRandomCpm component, will all these parent providers be pulled up under the parent instance?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
bromzh, 2016-04-04
@gleber1

No, of course, you only need to specify what you will embed directly into the component itself. And you can even not specify anything if you register the service globally in the bootstrap function.
In general, the logic is this: all services that are implemented must somehow be registered. Those services that you have registered globally in the bootstrap function will be available globally in all components. They do not have to be described in the providers field. If services are not registered globally, then they must be registered in the component. Moreover, services will be available in child components.
In general, each component has its own dependency injector. Thus, for each component, you can configure service providers in your own way. If the service is not registered with the component, then it is searched up the component hierarchy.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question