Reputation: 93
I am wondering why there is different technics to share data between components in Angular2 as explain here: https://angular.io/docs/ts/latest/cookbook/component-communication.html
For example, "input binding" is very easy to use. Why would I bother to "Parent and children communicate via a service"?
Is there performance issues? Or they are quite the same and it only depends on how it is easier to maintain.
Thanks,
Upvotes: 3
Views: 89
Reputation: 17337
Input binding is used for direct family (parent - child) while you can use services for other relationships. I believe this is a design choice.
Like here, they both can communicate because they are "touching" each other (wow that went weird really fast):
<parent>
<child/>
</parent>
Here you can use Input, Output, it's fast and elegant.
Where as
<parent>
<child>
<subchild></subchild>
</child>
<otherChild>
<otherSubChild></otherSubChild>
</otherChild>
</parent>
Here if you would like to communicate from otherSubChild to subChild you'd use a service because they are not closely related.
Altough, for brothers communication there is a neat trick : Catch event in sibling component
<parent (event)="sis.onEvent">
<bro/> <-- this is the guy emitting the event
<sis #sis/>
</parent>
So in other words, as long as they are "touching" (god, I need a better word), each other you don't have to rely on a service.
Upvotes: 1