0

I have multiple different services that extend another service:

BaseService {
    display(message: string): void { }
}

BetterService {
    constructor(private base: BaseService) { }

    display(message: string): void {
        this.base.display('Better ' + message);
    }
}

Now I configure them in AppModule like this:

imports: [
    DisplayModule.forRoot()
],
providers: [
    { provide: BaseService, useClass: BetterService }
]

Obviously, Angular fails because when it tries to resolve BaseService needed for BetterService, it still uses BetterService (in an endless recursion). What can I do to make it work?

Ivan Zyranau
  • 879
  • 2
  • 14
  • 33
  • This is not exactly a cyclic dependency. What error do you get? – Yaron Schwimmer Jan 23 '17 at 09:32
  • Can't resolve all parameters for BetterService: (?). – Ivan Zyranau Jan 23 '17 at 09:33
  • I don't think this is a duplicate of the question it is marked as a duplicate of although the solution might be similar. This question is about providing a BaseService to the BetterService, not really a cyclical dependency. If BetterService is the only class that needs a BaseService, you could just create an instance of it in your constructor instead of relying on dependency injection. – Jason Goemaat Jan 23 '17 at 09:54
  • I have a lot of modules that use BaseService. And I need to provide another service which will alter BaseService throughout the application using decorator pattern. – Ivan Zyranau Jan 23 '17 at 10:54

0 Answers0