Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to communicate between component in Angular?

I'm working on a web app project and I'm trying to use Angular, I get some problem with component communication. For example, how a parent component exchange data with child component, how to communicate between siblings components.

like image 377
吴桂林 Avatar asked May 28 '15 08:05

吴桂林


People also ask

How communication happens between component to component?

There are three ways: parent to child - sharing data via input. child to parent - sharing data via viewChild with AfterViewInit. child to parent - sharing data via output and EventEmitter.

How many ways we can communicate between components?

By using any of the following methods we can connect 2 components. using services. parent component calling viewchild. parent interacting with child using a local variable.


1 Answers

If you are trying to communicate from a parent component to a child component, this is pretty clearly described using @Input and EventEmitters with @Output in the angular docs.

Angular 2 component interaction

As for communication across siblings, I posted an answer in a similar question that might help with the issue of sharing data across sibling components. Currently, i think the shared service method is the most efficient.

angular-2-sibling-component-communication

like image 112
Alex J Avatar answered Sep 24 '22 07:09

Alex J