A
A
ar52017-01-25 15:22:31
JavaScript
ar5, 2017-01-25 15:22:31

Is it always necessary to load a service in Angular2?

Hello, I have a question. I have a ngTranslate service that is responsible for languages ​​and I loaded it in app.component in the main application component for switching. I have created another component that lies in the main component and I want to switch the language through it, so I must "inject" this service into it. But if I add it to the main component, can I give control to the child components?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
DTX, 2017-01-25
@DirecTwiX

The service must be specified in the component's Providers or at least one of its parent components.
I have all services connected in Providers of the main component (AppComponent).
If you add a service to the providers for each component, then the instances of the services when you insert (Inject'e) into the corresponding components will differ.

S
sergey, 2017-08-10
@Saberwalker

Well, something like this, of course, put your own plus / minus icons, but the code is working, even if you just click on dummies where there should be plus / minus, the number will change
jsfiddle

A
Andrey B., 2017-08-10
@andykov

Required. At a minimum, to handle clicks on + - and show / hide the block

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question