8

Is there a way get an instance of an injectable service of angular 2 just by the service name?

For Example, in Angular 1 you could write:

var service = $injector.get('ServiceName');

and the service variable would get an instance of the service.

I'd really appreciate your help guys!

Ron Avraham
  • 478
  • 2
  • 7
  • 18

2 Answers2

12

If you provide it by name, you can inject it by name

@NgModule({
  providers: [
      ServiceName, 
      {provide: 'ServiceName', useExisting: ServiceName}
  ],
  ...
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • How would I go about getting this service from a component? Using `Injector.get` is deprecated as of version 4 – Paradoxis Nov 15 '18 at 12:18
  • "Injector.get(token: any, notFoundValue?: any): any is now deprecated use the same method which is now overloaded as Injector.get(token: Type|InjectionToken, notFoundValue?: T): T;" https://github.com/angular/angular/blob/master/CHANGELOG.md#400-beta5-2017-01-25 – Günter Zöchbauer Nov 15 '18 at 13:00
  • Thanks for the info, but how would this be used in a component? I'm trying to upgrade the code from this answer to the 'new' format, but I can't wrap my head around the whole `InjectionToken` thing – Paradoxis Nov 15 '18 at 13:24
  • `InjectionToken` is just a key used to register a provider with and then to request an instance for this key with `.get(...)`. – Günter Zöchbauer Nov 15 '18 at 13:51
  • 2
    How would I get the key if it's passed as a string variable from a template? [Example of what I mean](https://stackblitz.com/edit/angular-3admbe) (check out hello.component.ts) – Paradoxis Nov 15 '18 at 14:03
  • I see what you mean. Should work as explained in https://angular.io/api/core/InjectionToken#description. – Günter Zöchbauer Nov 15 '18 at 14:10
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/183726/discussion-between-paradoxis-and-gunter-zochbauer). – Paradoxis Nov 15 '18 at 14:22
  • I'd suggest you create a new question. I'm quite out of practice with Angular. – Günter Zöchbauer Nov 15 '18 at 14:31
  • downvoting answer, as it's incomplete - does not show how to access such service, only how to define name for a service. – Maciej Miklas Jun 10 '21 at 06:36
  • @MaciejMiklas that's shown in the question. – Günter Zöchbauer Jun 11 '21 at 07:58
1

You can use Explicit injector creation using the Injector Class

injector = ReflectiveInjector.resolveAndCreate([ServiceClass, Dependency1Class, Dependency2Class]);

let service= injector.get(ServiceClass); //pass the type not the name
Fals
  • 6,813
  • 4
  • 23
  • 43
  • 4
    That wasn't what the OP asked. – shteeven Feb 26 '19 at 21:50
  • 1
    It's another option! I haven't contribute with stackoverflow anymore because of this kind of behavior. Options should be given. – Fals Feb 26 '19 at 21:53
  • Unfortunately, this is not an option. The question clearly asks "just by the service name?", so offering a solution that just ignores that restriction is not helpful. – O. R. Mapper May 07 '22 at 13:05