Passing data into "router-outlet" child components Passing data into "router-outlet" child components angular angular

Passing data into "router-outlet" child components


<router-outlet [node]="..."></router-outlet> 

is just invalid. The component added by the router is added as sibling to <router-outlet> and does not replace it.

See also https://angular.io/guide/component-interaction#parent-and-children-communicate-via-a-service

@Injectable() export class NodeService {  private node:Subject<Node> = new BehaviorSubject<Node>([]);  get node$(){    return this.node.asObservable().filter(node => !!node);  }  addNode(data:Node) {    this.node.next(data);  }}
@Component({    selector : 'node-display',    providers: [NodeService],    template : `        <router-outlet></router-outlet>    `})export class NodeDisplayComponent implements OnInit {    constructor(private nodeService:NodeService) {}    node: Node;    ngOnInit(): void {        this.nodeService.getNode(path)            .subscribe(                node => {                    this.nodeService.addNode(node);                },                err => {                    console.log(err);                }            );    }}
export class ChildDisplay implements OnInit{    constructor(nodeService:NodeService) {      nodeService.node$.subscribe(n => this.node = n);    }}


Günters answer is great, I just want to point out another way without using Observables.

Here we though have to remember that these objects are passed by reference, so if you want to do some work on the object in the child and not affect the parent object, I would suggest using Günther's solution. But if it doesn't matter, or actually is desired behavior, I would suggest the following.

@Injectable()export class SharedService {    sharedNode = {      // properties    };}

In your parent you can assign the value:

this.sharedService.sharedNode = this.node;

And in your children (AND parent), inject the shared Service in your constructor. Remember to provide the service at module level providers array if you want a singleton service all over the components in that module. Alternatively, just add the service in the providers array in the parent only, then the parent and child will share the same instance of service.

node: Node;ngOnInit() {    this.node = this.sharedService.sharedNode;    }

And as newman kindly pointed, you can also have this.sharedService.sharedNode in the html template or a getter:

get sharedNode(){  return this.sharedService.sharedNode;}


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.