0

I need help with how I can inject a service into another service that I have. Here is the setting:

this is my main.ts where I bootstrapModule:

platformBrowserDynamic()
    .bootstrapModule(DTIframeModule, {
        preserveWhitespaces: false 
    }).then((ngModuleRef: NgModuleRef<DTIframeModule>) => {

        const rootInjector = ngModuleRef.injector;


        const instrumentationService = rootInjector.get(InstrumentationService);

        if (instrumentationService instanceof DTService) {
            const routeService = rootInjector.get(DTRouteService);
            instrumentationService.registerDTRouteService(routeService);
        }
    }

and this the detail of InstrumentationService and DTService:

@Injectable()
export abstract class InstrumentationService {
    public abstract generateDTEvent(
        eventName: string,
        params?: Params): InstrumentationEvent;
}

@Injectable()
export class DTService implements InstrumentationService {
    private queuedEventsPopulatedWithRoute: boolean;
    // few other fields

    public registerRouteService(routeService: RouteService) {
       // some function
    }

    public generateDTEvent(
        eventName: string,
        params?: Params): InstrumentationEvent {
        // some service
        }
}

What I have issues with is that I would like to use a service called WinService inside DTService, I added the import:

import { WinService } from '../services/win.service';

and wanted to access it inside DTService like this:

this.winService.native.events;

But however I tried I am hitting the wall and getting error that this.winService is undefined. I tried adding a constructor to DTService and add private readonly winService: WinService and it is throwing injection error. Any help would be very much appreciated. Thanks!

2
  • Did you add the WinService in the providers array of the module? Commented Aug 9, 2018 at 3:46
  • What does WinService look like? How does your constructor of DTService look like when you import it? Commented Aug 9, 2018 at 4:26

1 Answer 1

1

Please add WinService in to providers array of the module.

If it's provided in another module, please import that module to your module where you have provided DTService

Please read Angular Dependency Injection here for more information.

Sign up to request clarification or add additional context in comments.

5 Comments

it is not directly included in the Module and it has been used in other part of the angular app, but when I try to add it DTService which is used inside BootstrapModule, it throws error. Do you recommend adding it to providers list in the main app module or instrumentationModule?
Both cases will work. But according to your business requirement you have to decide where you should provide it. If it's more related to instrumentationModule put it there. Else put it at app module.
put it on the main module, { provide: WinService }, { provide: ErrorHandler, useClass: DTPageErrorHandler }, and this.winService is used inside ErrorHandler and it throwing error that it is undefined. It wasn't throwing this error before and winService was working fine inside ErrorHandler
in place of { provide: WinService } try using WinService
If problem's still there please provide more code so I can review and give comments

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.