1

I have two components, a list and a detailed view. On clicking an item on the ListComponent I'd like to hand the current data set over to the Detailed component. The idea behind this, is that if the data is already available the DetailedComponent does not has to refetch it through the service.

Is it possible to add an object to routerLink which the DetailedView than gets onInit()?

eg.

router

...
{ path: 'hero/:id', component: HeroDetailComponent }

ListComponent

onSelect(hero: Hero) {
  this.router.navigate(['/hero', hero.id]); // how to add hero as heroData
}

DetailComponent

ngOnInit() {
  if( this.route.params.heroData ) {
    this.hero = heroData // < this is what i like to do.
  } else {
    this.route.params
      // (+) converts string 'id' to a number
      .switchMap((params: Params) => this.service.getHero(+params['id']))
      .subscribe((hero: Hero) => this.hero = hero)
  }
}

I do know about angular 2 custom route parameters however they expose all the data and make it manipulatable or even old data is used if the url is kept. Thus this is not the they I'd like to go.

Manuel
  • 9,112
  • 13
  • 70
  • 110
  • Yes. you can pass data to a nested component (from parent to child). You can also passing data from a component to another using different methods. you can check the official documentation to choose the one that you can find simple (https://angular.io/docs/ts/latest/cookbook/component-communication.html) – Mourad Idrissi Dec 23 '16 at 11:30
  • You can also refer on this answer, is more particular http://stackoverflow.com/questions/36835123/how-do-i-pass-data-in-angular-2-components-while-using-routing – Mourad Idrissi Dec 23 '16 at 11:33
  • Shared service is probably what you are looking for. Check these: http://stackoverflow.com/questions/35478994/angular-2-passing-object-via-route-params-possible and http://stackoverflow.com/questions/36994918/passing-objects-in-routing – AT82 Dec 23 '16 at 12:04

0 Answers0