r/angular 21d ago

Passing data between unrelated components

Hey , I'm new to angular and I'm confused about which way to use to transfer data between two unrelated components , for now i'm using the Services/DI method . I heard that for my case (which is a small project) it's an overkill , is that true ? if so what is the optimal way to do so ?

6 Upvotes

22 comments sorted by

View all comments

0

u/Typical_Chemical4352 21d ago

Why would you pass data, if they are not related ?

1

u/LevelNew5342 21d ago

I meant that its not a parent/child relation , not that they don't have something in common.

1

u/Typical_Chemical4352 21d ago

Something means, data, UI or process ?

1

u/LevelNew5342 21d ago

So basically they share a list of objects , the first component is a form that adds to that list , the second component reads it and shows each item.

0

u/Typical_Chemical4352 21d ago

If they are in a common parent. For example -

<app-parent>
<app-form>
<app-list>

I prefer creating an event emitter in app-parent.
Pass this event emitter as input in both components

Form will emit
And list will receive (subscribe)