The Problem
Normally, in Angular 2, one would listen to an event via the following syntax:
<my-elem (customEvent)="customEventProcessor"></my-elem>
But when I use a router, that host - <my-elem> - does not exist in any template. Instead, there's a <router-outlet>, and my component is loaded upon navigation. Thus, the crux of my problem is, How can I force my host to listen to my custom event without relying on a template?
Optional Details
Suppose I have some element list-view, which is a child of my root component. list-view listens for a custom event via the normal syntax:
<list-view (customEvent)="customEventProcessor()"></list-view>
Just for completeness, the list-view component that emits the event also has a predictable structure:
<button (click)="onDetailsClick(propertyOfInterest)">Click here</button>
The list-view sends the event up to the parent via observation.
class ListView {
...
public onDetailsClick(property: string): void {
this.customEvent.next({ value: property });
}
}
and that event triggers the customEventProcessor() function. So far so good. However, when I use a router to control whether list-view is present, I cannot (to my knowledge) insert a command to monitor some event.
I am not sure what the best approach is to handle this case.
@Output / outputs : []and to listen you can use@HostListener / host : {}