Check out a free preview of the full Build Web Apps with Angular 2 course:
The "Services Q&A" Lesson is part of the full, Build Web Apps with Angular 2 course featured in this preview video. Here's what you'd learn in this lesson:

Lukas answers a few audience questions about the @Injectable metadata decorator and injecting multiple services.

Get Unlimited Access Now

Transcript from the "Services Q&A" Lesson

[00:00:00]
>> [MUSIC]

[00:00:03]
>> Lukas Ruebbelke: Yes, sir?
>> Speaker 2: What would you ever put in for metadata in an injectable decorator there?
>> Lukas Ruebbelke: So, I think you could do additional providers in here. That's a good question.
>> Speaker 3: So, the injectable decorator is only needed if you want to inject other services into that service.

[00:00:23] So, in this case, Lukas isn't injecting anything into that service, so if he got rid of injectable it would actually still work.
>> [INAUDIBLE]
>> Speaker 3: But if he were to put a service in that service, then he would need injectable.
>> Lukas Ruebbelke: Well, there's some confusion, I have some confusion around that.

[00:00:35] I think the document, actually, is not crystal clear about that. So, Pascal, the way he writes in his blog post, you would think that you only need injectable if you need to inject something into it. But the document says that injectable, it makes that service available to the injector to be injected into other things.

[00:00:58] For injection. And so, that's, I will be the fist person to admit that I'm confused.
>> Scott: Beta.
>> Lukas Ruebbelke: Yeah, so to that I just say beta.
>> Scott: But you should always do injectable because you never know, if you write this in third party module, you never know you if somebody's gonna extend this, or like you have to go back.

[00:01:18] So just always do it because it won't hut to add it. But It will hurt if you don't have it
>> Lukas Ruebbelke: Scott just reached in and pulled me from the abyss. Thank you. Nice save. Are you ready to talk about routes or what?
>> Speaker 2: Well, there's a question here.

[00:01:32] Is there a way to inject the pricing service without putting it at the root app component level?
>> Lukas Ruebbelke: So, I believe you can go
>> Lukas Ruebbelke: Does this take?
>> Lukas Ruebbelke: I'm getting all greens, so I'm thinking
>> Speaker 5: So, by doing that gonna need an instance.
>> Lukas Ruebbelke: Yeah, so I think you can inject it right in here.

[00:02:01] Let me just check real quick. Angular io.
>> Lukas Ruebbelke: This is where muscle memory,
>> Lukas Ruebbelke: Injectable metadata.
>> Lukas Ruebbelke: You may, the way that I'm reading it, actually have to inject it into a top level component for it to be available for your service. It doesn't look like this actually takes any parameters, which is interesting.

[00:02:45]
>> Speaker 3: Yeah, the injectable director doesn't take any injectables, which is weird cuz it's name is injectable.
>> Lukas Ruebbelke: That just seems like hypocritical. I want to be injected, but I'm not accepting, seems rather one way. So, anyways, I, apparently at this point, in order to get a service available to another service, you have to inject it at some top level component.

[00:03:17] If I find out anything different, I'll let you know.
>> Speaker 6: So, some random thing on the Internet said if you used component you don't need to use injectable.
>> Speaker 6: So maybe you can use component and give it providers.
>> Lukas Ruebbelke: Interesting. If you could find that random thing, post a link somewhere.

[00:03:40] That would be helpful. We are a community. A child does not raise itself.
>> Speaker 3: The other way, there is a way to get your service. So, you just have to have injectable in your service. And then the service that you wanna inject, just annotate it inside the constructor of the service that you're trying to inject it into, and it should work.

[00:04:01]
>> Lukas Ruebbelke: And you can annotate it directly in the constructor?
>> Speaker 3: Yeah, you can enter it directly into the constructor. As long as you import it in the top of the file, you should be fine.