Fully rephrased question:
I tried to simplify the problem, cause I understand the way I initially wrote it down is pretty convoluted; what follows is a simpler representation of the problem, albeit a slightly different representation of the problem.
I need a way to create two instances of a service in a top level component (namely the “app” component), and to pass each instance down to two different instances of another component (namely the "Client" component):
- The first instance of the "Client" component will be rendered within a
<router-outlet>; - The second instance of the "Client" component will be rendered within a MatDialog;
The service itself will just hold two methods and two observables in order to achieve parent-child communication, and implementation shouldn’t really matter in this case, but it would go something like this:
interface ChildInput {
someData: number;
}
interface ParentInput {
someData: string;
}
@Injectable
export class ComService {
pushToChild(data: ParentInput) {
// code to push data to the "child" observable
}
pushToParent(data: ChildInput) {
// code to push data to the "parent" observable
}
// code to instantiate two observables, one for the parent to subscribe to and one for the child to subscribe to
}
What should be noted though, is that I need to track down which “child” component is sending which data; hence a shared instance of the service won’t work in this case.
Here's how the top-level component's template is structured:
<a [routerLink]="['/new-client']">
New client
</a> <!-- this will render the "Client" component within router-outlet -->
<router-outlet></router-outlet>
<button (click)="openClientPopup()">
Add new client
</button> <!-- this will render the "Client" component within a MatDialog instance created by the openClientPopup() method -->
As I said creating a singleton instance at the top level component and injecting it into the "child" module the classic way won't work, because each instance of the "child" module will exchange data with the parent that should be bound, in the parent, to that specific instance of the component itself, and tracking down the sender by using some sort of id or something like that feels unnecessarily convoluted (It might not be though if that's the only way to go about doing it; I really don't know, and that's why I'm asking into first place);
On the other hand, while I could easily pass a service instance down to a component rendered within a MatDialog (there are actually multiple ways to do it), I'm not aware of a way to pass an instance of a service down to a component rendered within a <router-outlet>.
Any suggestion?