Based on the docs of Angular 2 you can pass data from a component to another quite easily with the @Input.
So for example I can set the parent like this:
import { Component } from '@angular/core';
import { HEROES } from './hero';
@Component({
selector: 'hero-parent',
template: `
<h2>{{master}} controls {{heroes.length}} heroes</h2>
<hero-child *ngFor="let hero of heroes"
[hero]="hero"
[master]="master">
</hero-child>
`
})
export class HeroParentComponent {
heroes = HEROES;
master: string = 'Master';
}
And the get the data in the child component like this:
import { Component, Input } from '@angular/core';
import { Hero } from './hero';
@Component({
selector: 'hero-child',
template: `
<h3>{{hero.name}} says:</h3>
<p>I, {{hero.name}}, am at your service, {{masterName}}.</p>
`
})
export class HeroChildComponent {
@Input() hero: Hero;
@Input('master') masterName: string;
}
So it's quite obvious, pass the [hero]="data"
to the template of the parent component (of course in the child selector) and handle them into the child component.
But here's the issue.
My child component DOM element (in this case <hero-child>
) is not available in the parent's template but instead it is loaded there in a <router-outlet>
element.
How can I pass data then to a child route component then? Isn't the input the correct way? I want to avoid double,treble etc calls to get the same data that already have in my parent route/component.