angulartypescriptangular2-servicesangular-services

Angular - inject in other way than constructor?


In Angular, I have a service that has few things injected through the constructor(...). However that service is also at some place created by calling the constructor. Therefore, adding another service it depends on to the parameters would change the API. I'd like to avoid that.

Is there a way to inject a service into another service without adding it to the constructor parameters? E.g. field injection?

import {Inject, Injectable} from "@angular/core";
import {
    Http, Request, ConnectionBackend, RequestOptions, RequestOptionsArgs, Response, Headers,
    RequestMethod
} from "@angular/http";

import {KeycloakService} from "./keycloak.service";
import {Observable} from 'rxjs/Observable';
import {EventBusService} from "../events/event-bus.service";
import {LoadingSomethingFinishedEvent, LoadingSomethingStartedEvent} from "../events/windup-event";

@Injectable()
export class WindupHttpService extends Http {
    constructor(
        _backend: ConnectionBackend,
        _defaultOptions: RequestOptions,
        private _keycloakService: KeycloakService,
        // ----- This is what I want to avoid. -----
        private _eventBus: EventBusService,
    ) {
        super(_backend, _defaultOptions);
    }

    // -------  This is what I am looking for ---------
    //@Inject()
    //private _eventBus: EventBusService;

Solution

  • Yes and no.

    You can use the Injector, but the best way is to inject it into the service:

    constructor(private injector: Injector) {
            let httpService: Http = this.injector.get(Http);
    }
    

    More about Injector can be found here: https://angular.io/api/core/Injector

    Here is also usable link, as @DBosley mentioned: https://angular.io/guide/dependency-injection#appendix-working-with-injectors-directly