Imagine a scenario where there is a parent component called AppComponent
, a directive named MyDirective
, and a service named SimpleService
.
In this case, MyDirective
is utilized in the template of AppComponent
and injects SimpleService
using the Host
decorator. To avoid using the instance from the parent of AppComponent
, a new instance is created within the viewProviders
of AppComponent
.
However, a situation arises with the introduction of a child component called HelloComponent
under AppComponent
. Now, HelloComponent
also uses MyDirective
, but in this case, I want the MyDirective
inside HelloComponent
to utilize the same instance of SimpleService
from AppComponent
. How can this be achieved?
I attempted utilizing
{provide: SimpleService, useExisting: SimpleService}
, but it resulted in a cyclic dependency error.
Sample code: https://stackblitz.com/edit/angular-7h352z?file=src%2Fapp%2Fhello.component.ts