12

Dears, The Golden Rules about web component interaction are:

  • Parent references direct children
  • Children doesn't reference parent
  • Parent -> Child: via method calls
  • Child -> Parent: via events

This pattern worked for us in many projects (YUI, JQuery, ...) What about Angular2?

Should we use:

or Input/Output?

First choice looks beautiful. @angular2 @expert, What do you think about?

Thx to all, Gabriel

Gabriel
  • 121
  • 1
  • 4
  • My personal preference is the Input/Output. Pass the data to the child via input and emit the events for parents to listen to via output. – Anjil Dhamala Jan 30 '18 at 16:41

2 Answers2

2

It depends as per your need. Just to be clear Output and events are the same. So the flow is quite similar:

  • Parent passes data to child via Input
  • Child passes data to parent via Output which EventEmitter

There is a third option also where you can communicate both ways from child to parent and parent to child or even one component to other. It is using service injection, where your service will have a Subject and component injecting this service can subscribe or update the subject value.

Sumeet Kale
  • 365
  • 1
  • 11
-1

I think Service Method would be best because I know many people find familiar working with this and is a good way to feel you are in home.If you are using Angular material theme then you can't use directly @input() @output() and @viewchlid method for component's communication.

Unheilig
  • 16,196
  • 193
  • 68
  • 98