6

I'm currently trying to build an Angular2 prototype (based on alpha44) of our Angular1 app (pretty complex one) and I'm trying to find the best model/data architecture when using routes and child routes.

In my example, from a child component created from a route, I want to access a property of the parent component (hosting the router-outlet).

But when you create a component from a router-outlet, you cannot use @Input and @Output anymore. So what is the best practice to inject some data/properties, except basic routeParams and static routeData?

How do you communicate with the parent component without too much coupling?

Benoit Hediard
  • 334
  • 1
  • 3
  • 8
  • 2
    Look at [this issue](https://github.com/angular/angular/issues/4452). AFAIK now the only way to communicate with component from `router-outlet` is by using DI. – alexpods Oct 23 '15 at 22:35

3 Answers3

2

You can use RouteData in order to pass data into routes. See here and here. I'm still missing the part of initialising this data obj from the component (see my question regarding)

Community
  • 1
  • 1
Yaniv Efraim
  • 6,633
  • 7
  • 53
  • 96
2

A shared service can be used with components added by the router. For details see https://angular.io/docs/ts/latest/cookbook/component-communication.html

data support also was added to the new router in RC.4. For details see How do I pass data in Angular 2 components while using Routing?

Angular 2 - equivalent to router resolve data for new router might also be related.

Community
  • 1
  • 1
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
0

You can pass (inject) data from child component inside Router Outlet to Parent component with Subject,Observable. You can found my solution in this video.

See the code on GitHub: https://github.com/tabvn/angular-blog

Roger Ng
  • 771
  • 11
  • 28
Toan
  • 7
  • 2