Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2: output/input versus event/ViewChild - Component interactions

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:

  • Parent calls a ViewChild
  • Parent listens for child event

or Input/Output?

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

Thx to all, Gabriel

like image 828
Gabriel Avatar asked Jul 14 '16 16:07

Gabriel


1 Answers

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.

like image 81
Sumeet Kale Avatar answered Oct 14 '22 07:10

Sumeet Kale