We use the @Input() decorator to pass data from the parent to the child component via the template of the child component.
<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.
import {Injectable, EventEmitter} from "@angular/core";
@Injectable()
export class DataService {
onGetData: EventEmitter = new EventEmitter();
getData() {
this.http.post(...params).map(res => {
this.onGetData.emit(res.json());
})
}
import {Component} from '@angular/core';
import {DataService} from "../services/data.service";
@Component()
export class MyComponent {
constructor(private DataService:DataService) {
this.DataService.onGetData.subscribe(res => {
(from service on .emit() )
})
}
//To send data to all subscribers from current component
sendData() {
this.DataService.onGetData.emit(--NEW DATA--);
}
}
There are 3 ways to pass data from Parent to Children
Through Children Router Resolver if you have to receive different data
this.data = this.route.snaphsot.data['dataFromResolver'];
Through Parent Router Resolver if your have to receive the same data from parent
this.data = this.route.parent.snaphsot.data['dataFromResolver'];
Note1: You can read about resolver here. There is also an example of resolver and how to register the resolver into the module and then retrieve data from resolver into the component. The resolver registration is the same on the parent and child.
Note2: You can read about ActivatedRoute here to be able to get data from router
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With