Angular2: data binding with routing children

2019-07-14 12:35发布

问题:

I have a simple setup. I'm using a routing component with multiple child components which are loaded dynamically depending on the route. I'd like for some of these child components to have data-binding with its routing parent.

I'll add an @Output() property to the child, but how can I make the routing parent listen for the events that this event-emitter emits?

Normally in a template with a statically defined component I'll do something like this:

<child (myevent)="handleEvent($event)">

and the parent's handleEvent method will handle the event.

But since routing insert the components dynamically, this is not possible. Is there a way to make it work? I know I can use a service to achieve the same result, but I'd like to avoid that in this case.

Thanks

回答1:

This is currently not supported. You can use a shared service to share data with dynamically inserted children. The service can use Observable to allow child or parent to subscribe to changes and get actively notified.