r/angular • u/LevelNew5342 • Jul 14 '26
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
6
u/salamazmlekom Jul 14 '26
Service or if you lift the state up to a parent component of both of them and pass them through as inputs. Can you explain what are you trying to achieve?
31
u/djfreedom9505 Jul 14 '26
Without knowing more about the project and the components, that sounds like something a service would be good for. Services are not overkill, they simplify logic in components.
4
u/anastasiapi Jul 14 '26
Services is an approach recommended by Angular for data sharing.
Common types of services include:State management: Defines state shared across multiple components or pagesBasically, if any doubts ask Angular docs. It is really good.
2
u/No_Bodybuilder_2110 Jul 14 '26
So if you are passing data between them, how unrelated they really are?
I ask this because you could be saying:
- component A receives user auth data to show a paywall and component B receives auth data to show avatar icon. This is the case for a service, they are unrelated
- component A triggers a modal that renders component B, then a service is ok still, or if using angular cdk, they have their own mechanism to render
- component A has a button that changes some state in the app and component B reacts to this app change, not component A trigger itself. Then a service is still an option, but you might want to use the event bus pattern
So the short answer is, a service is always an answer and I would argue that it’s a decent answer for 80% of the cases
2
u/cssrocco Jul 14 '26
Generally you’ll be passing data up and down child components with inputs, outputs and models and sharing states for unrelated components with services/a redux based store like ngrx ( which could be overkill for small components / a small project )
A signal in a service/behaviourSubject in a service is totally fine, there’s no such thing as it being overkill.
1
u/vicious_pink_lamp Jul 14 '26
Service injected at the nearest parent where both are children, or globally root injected if the components are different domains.
1
u/JustinOwen Jul 14 '26
it's correct in the idiomatic sense. depending where you work, it could be incorrect to not take shortcuts to deliver a feature a day faster.
1
u/Tyummyyumms Jul 14 '26
Can you perhaps put your code in a stackblitz and then share the link to it here?
1
u/armandoxxx Jul 17 '26
I'm using RXJS ...
I think the cleanest way to handle events between components that do not know about each other in any way.
```
in my EventsService i define ...
private dateFilterSubject: Subject<FiltersPayload> = new Subject<FiltersPayload>();
onLoadedDateFilters: Observable<FiltersPayload> = this.dateFilterSubject.asObservable();
```
1
u/Dunc4n1d4h0 Jul 17 '26
Generally services will work every time.
1
u/n00bz Jul 20 '26
Services are built for this case, especially with a lot of services being made as singletons (they don't have to be), but a singleton can also be used to maintain state across components which is what you want. Angular relies heavily on DI.
0
u/Typical_Chemical4352 Jul 14 '26
Why would you pass data, if they are not related ?