angular.io icon indicating copy to clipboard operation
angular.io copied to clipboard

Communication between two sibling components in angular 2

Open nrmaridu opened this issue 10 years ago • 19 comments

I am right now working on angular 2. I found that the communication between the two sibling components or from child to parent is difficult. Even it was not mentioned in the angular 2 documentation. Would you please provide some information about it because the way two sibling components communicate is looking complex than the way a parent component communicates with its child using the 'ViewChild' decorator. But it is not possible to make a parent child relationship between two components always as per the project requirements.

Please find the below scenario of component tree. A /
B C / \ /
D E F G Here If I want to communicate between the components 'A' and 'B' then I can use 'ViewChild' decorator because they both are in parent child relationship. If I want to communicate between the components 'B' and 'G' then How to do it? Because I can't impose parent child relationship between those two.

Would you pleas address the above issue.

nrmaridu avatar Oct 24 '16 18:10 nrmaridu

https://angular.io/docs/ts/latest/cookbook/component-communication.html#!#bidirectional-service

zoechi avatar Oct 31 '16 09:10 zoechi

I already read this..But this is not we want. Angular 2 documentation didn't mention properly about siblings communication except the communication thorough event emitting. I saw in one video that angular 2 team is building production web sites already. I wonder like How come they didn't face this basic issue?

nrmaridu avatar Nov 02 '16 11:11 nrmaridu

The documentation goes quite some length about using services to communicate. This is how it's done in Angular2 when the elements are not direct parent-child.

zoechi avatar Nov 02 '16 11:11 zoechi

@zoechi But this documentation you posted: https://angular.io/docs/ts/latest/cookbook/component-communication.html#!#bidirectional-service

talks explicitly about 'Parent and children communicate via a service' and not sibling to sibling!

Is that link still correct? Ok... concerning that there must be always a root/parent component for those siblings there exist no real sibling communication. Its always a parent/child communication ;-)

bastienJS avatar Nov 16 '16 10:11 bastienJS

Sibling to sibling is just two children communicating to the same parent (parent is mediator) or like two independent components using a shared service. You can use a template variable to refer to a component on a sibling element from within the same template. Perhaps it could be stated in the docs that there is no special support for sibling-to-sibling communication.

zoechi avatar Nov 16 '16 11:11 zoechi

Implemented by using shared service. Look at the demo https://embed.plnkr.co/P8xCEwSKgcOg07pwDrlO/

chennamouli avatar Feb 07 '17 22:02 chennamouli

@chennamouli - I have implemented exactly similar as shown in demo. But in my project, only publisher component receives response from service. i.e. if sibling1 publish data then sibling1 will only get data back and if sibling2 publish data then sibling2 will receive data. looks like each component is having its own instance of service. what am I missing? please help.

Update: there was provider declaration of shared service in one of the component separately which caused that issue. After removing it, it worked. Thanks for providing demo example.

rpp24 avatar Mar 27 '17 17:03 rpp24

How can this work on a routed component environment. In the above plunker example the app component template contains the child component selectors, that's why it's working, but in case I am using component router there will be no selectors present at the main AppComponent. Then how will it work?

ayanb1991 avatar Apr 12 '17 03:04 ayanb1991

i've the same situation too what Ayanb1991 is asked.. Please help.

rajachennupati avatar Apr 15 '17 21:04 rajachennupati

@ayanb1991 @rajachennupati GitHub issues are for bug reports and feature requests. For support questions please use other channels like the ones listed in CONTRIBUTING - Got a Question or Problem?

zoechi avatar Apr 16 '17 10:04 zoechi

There should not be an issue with router. It is working fine in my application with router.

chennamouli avatar Apr 17 '17 21:04 chennamouli

i ended up with no luck configuring shared service with router configuration in my app . @chennamouli - could you please help with some demo how this works . thanks in advance .

vkirancv avatar May 10 '17 20:05 vkirancv

@Jaithra GitHub issues are for bug reports and feature requests. For support questions please use other channels like the ones listed in CONTRIBUTING - Got a Question or Problem?

zoechi avatar May 11 '17 02:05 zoechi

Creating a service for passing a Height of an element, from one sibling to another is such an over kill. NG team needs to offer a solution for that. Accessing simple Dom properties of siblings is a very common task.

neoswf avatar Aug 17 '17 17:08 neoswf

I stumbled upon this issue and could think of this solution. Let's say you've got two sibling components menu and panellisting:

<div>
  <menu [panel]="panel"></menu>
  <panellisting #panel></panellisting>
</div>

Then in menu component make panel to be an @Input():

export class MenuComponent implements OnInit {
    @Input() panel: any;
    ...
}

From this moment on I could access all panel component functions/properties from within menu component via this.panel.

I suppose you can always try to do the vice-versa, not sure if this is correct or has performance impact though.

todorpr avatar Sep 27 '17 10:09 todorpr

is there a working solution for this yet? i have a scenario where i have two components A and B

in my view there are 5 As and each do the same task say calculate a price although for different item at different rates and then B consolidates and all of A's data and project it. i have a setup like this

<div class="ui container">
  <app-header></app-header>
  <div class="ui grid">
    <div class="ui row">
      <div class="ui twelve wide column">

        <div class="ui two link stackable cards">
          <app-stat-card title= {{resource}} *ngFor="let resource of resources">  </app-stat-card>
        </div>
      </div>
      
      <app-overall-stats-card></app-overall-stats-card>
    </div>
  </div>
</div>

the only value passed from A to B is "price", a single variable. creating service for this is, as @neoswf suggested overkill. kindly let us know how to do it. I think this needs to be a feature request.

Ragzzy-R avatar Nov 17 '17 11:11 Ragzzy-R

@Ragzzy-R Here the components A and B must be the child components of some other component(parent) right? So you can consolidate all the data in that parent component and can pass it as input parameter to component B. Does that solves your problem?

nrmaridu avatar Nov 17 '17 12:11 nrmaridu

ya that might work. but i was wondering if we could cut short parent's intervention. is it a design decision to always have parent as mediator?

also since each is independent, how do u keep track of all of their data? each app-stat-card changes value of "price" independent to each other at random times. how do I keep track of them? i know i can get their data but what i want is at any given time if the price is $100 i need to know from which stat-card that came from. It may be silly but im too new. if its not advisable to talk about troubleshooting/help in issue tracker, you can mail me to [email protected]. thanks! :smile:

Ragzzy-R avatar Nov 17 '17 12:11 Ragzzy-R

Hey guys, I am going to close this issue. Let's continue our discussion in stackoverflow.com or gitter as that is suggested by angular team. Happy coding('.')

nrmaridu avatar Nov 17 '17 12:11 nrmaridu