3

I have 2 Components, they are not child-parent components, just to independent components. I would like to pass data from Component1 to Component2. Is it possible? How could I implement this? Could you please give me an example or maybe a link to an article? I found a lot of articles, but they are all about parent-child components.

Anna F
  • 1,583
  • 4
  • 22
  • 42
  • 3
    *"give me an example or maybe a link to an article"* is not a good SO question. If you want communication between components beyond direct events, look into services; the Angular documentation gives an example https://angular.io/guide/component-interaction#parent-and-children-communicate-via-a-service – jonrsharpe Aug 13 '17 at 22:23
  • 1
    Use a service and inject it in both components. For more details, see [Component communication](https://angular.io/guide/component-interaction). – Lazar Ljubenović Aug 13 '17 at 22:24
  • 4
    Possible duplicate of [Angular 2 Sibling Component Communication](https://stackoverflow.com/questions/35884451/angular-2-sibling-component-communication) – Lazar Ljubenović Aug 13 '17 at 22:25
  • have you consider of using https://github.com/ngrx ? – angularrocks.com Aug 14 '17 at 04:30
  • try to use redux for angular https://github.com/angular-redux/ng-redux – Gregor Doroschenko Aug 15 '17 at 13:38

1 Answers1

1

You have two options here to pass data between components if they donot have a parent child relationship

  • Shared Services
  • Ngrx Suite (Redux)

Now when to use when if you application size is medium to small go for shared services using behaviour subject or replay subjects. please check this link to know more about behaviour subject and how to use shared services to communicate between components.

Using Ngrx is like a complete long term solution to this problem. It follows the redux pattern to solve state management in Angular. follow this link for more on ngrx and how to use them in a project .

More on ngrx

This is a live project using the whole ngrx suite state management footballapp

Rahul Singh
  • 19,030
  • 11
  • 64
  • 86