Yes, you can pass data directly into router outlet components. Sadly, you cannot do this using angular template binding, as mentioned in other answers. You have to set the data in the typescript file. There's a big caveat to that when observables are involved (described below).
Here's how:
(1) Hook up to the router-outlet's activate
event in the parent template:
<router-outlet (activate)="onOutletLoaded($event)"></router-outlet>
(2) Switch to the parent's typescript file and set the child component's inputs programmatically each time they are activated:
onOutletLoaded(component) {
component.someProperty = 'someValue';
}
Done.
However, the above version of onOutletLoaded
is simplified for clarity. It only works if you can guarantee all child components have the exact same inputs you are assigning. If you have components with different inputs, use type guards:
onChildLoaded(component: MyComponent1 | MyComponent2) {
if (component instanceof MyComponent1) {
component.someInput = 123;
} else if (component instanceof MyComponent2) {
component.anotherInput = 456;
}
}
Why may this method be preferred over the service method?
Neither this method nor the service method are "the right way" to communicate with child components (both methods step away from pure template binding), so you just have to decide which way feels more appropriate for the project.
This method, however, avoids the tight coupling associated with the "create a service for communication" approach (i.e., the parent needs the service, and the children all need the service, making the children unusable elsewhere).
In many cases this method also feels closer to the "angular way" because you can continue passing data to your child components through @Inputs. It's also a good fit for already existing or third-party components that you don't want to or can't tightly couple with your service.
On the other hand, it may feel less like the angular way when...
Caveat
The caveat with this method is that since you are passing data in the typescript file, you no longer have the option of using the pipe-async pattern used in templates (e.g. {{ myObservable$ | async }}
) to automagically use and pass on your observable data to child components.
Instead, you'll need to set up something to get the current observable values whenever the onChildLoaded
function is called. This will likely also require some teardown in the parent component's onDestroy
function. This is nothing too unusual, there are often cases where this needs to be done, such as when using an observable that doesn't even get to the template.