6

Is there a way to inject a service into a class without having to declare it as a parameter on the constructor?

The usual way works just fine, I have no complains about it. I'm just trying to create a super class to handle some stuff witch depends on a service and I want it to be independent, I don't want to have any references to that service on child classes.

This is the usual way:

export class MyComponent {
  constructor(private someService: SomeNpmInstalledService) {}

  doSomething() {
    this.someService.do();
  }
}

That's what I'm looking for:

export class MyComponent {
  @Some injection magic annotation here
  private someService: SomeNpmInstalledService;

  constructor() {}

  doSomething() {
    this.someService.do();
  }
}

I'll try to explain the reason why I want this kind of functionality with the following example:

export class MyComponent extends MySuperClass {
  constructor() {
    super();
  }

  doSomething() {
    super.someSuperClassFonctionality(); 
  }
}

export class MySuperClass {
  constructor(private someService: SomeNpmInstalledService) {}

  someSuperClassFonctionality() {
    someService.doStuff(); 
  }
}

The above code gives an error because in my component, when I call super(), I have to pass the parameter defined on the constructor of my super class. But That's exactly what I don't want to do. I want my components to don't even know about the service that is used inside the super class.

5
  • How you gonna use it without injecting in constructor? Commented Oct 2, 2017 at 16:53
  • 1
    Possible dupe: stackoverflow.com/q/45225676/3001761 Commented Oct 2, 2017 at 16:59
  • Thanks jonrsharpe. It's almost the same yes, but I'n my case I would need the injector to be injected in an other way then be declared in the constructor (if possible) Commented Oct 4, 2017 at 16:35
  • I ran into this issue a couple of projects before. I ultimately decided that there was no clean way of doing this, and I instead just made two services and injected the "superclass" service into the "subclass" service in its constructor and used it as a parallel service. Commented Oct 4, 2017 at 22:32
  • Thanks for the reply hayhorse. That was my backup approach too but was wondering if someone else had a nice alternative to it. Commented Oct 5, 2017 at 17:44

1 Answer 1

1

Yes: use Injector. https://angular.io/api/core/Injector

import { Injector } from '@angular/core';

then in your constructor:

constructor(injector: Injector) {
  let this.service = injector.get(ServiceToInject);
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your replay. It's almost what I need, but having the injector in the constructor is still a problem to my need. I edited my original question to add more detail.
He still have to inject "Injector" via the constructor
An argument for 'injector' was not provided .. =( I think I'm missing something

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.