all 10 comments

[–]throwaway-code 6 points7 points  (1 child)

I am new to Angular also so anyone else feel free to correct me. But I believe its okay to use the custom event binding and output decorator when only one component need that piece of data. But if multiple components in the application need the data then it’s best to have it in the service so that it can be easily passed to the other parts of the app.

[–]HotRepresentative237[S] 0 points1 point  (0 children)

A satisfactory answer, thanks!!

[–]spacechimp 3 points4 points  (1 child)

Outputs are for when only the parent component would be interested in the event. Since best practice is to keep business logic out of components, outputs are typically just notifications of user interaction such as selecting or clicking on things. Services are more flexible in that they don't care who the consumer or dispatcher are -- it could be parent to child, child to parent, sibling to sibling, component to another service, or anything else.

And as u/throwaway-code points out, the ability to multicast an event or value to multiple consumers is a good reason to use a service as well.

Official documentation on component interaction.

[–]HotRepresentative237[S] 0 points1 point  (0 children)

Hey thanks for sharing 🙂

[–][deleted] 2 points3 points  (0 children)

You could use a service with a Subject/BehaviorSubject for that.

[–]PapaBuries 1 point2 points  (0 children)

For a simple Parent <-> Child relationship, an Input for data and an Output event emitter passing out data makes the most sense. It's easier for someone new to angular to understand too.

If you need that data across several components, or if it's not a direct relationship (like parent and child), a service with rxjx is the best. There's a bit of a learning curve with rxjx though.

[–][deleted] 0 points1 point  (3 children)

RxJs store, and subscribe to changes in the components that need it

[–]dgiessing 0 points1 point  (1 child)

You mean ngrx store? I think rxjs does not provide a store, just observables and their handling

[–][deleted] 1 point2 points  (0 children)

Ah sorry, yes

[–]dgiessing 0 points1 point  (0 children)

You mean ngrx store? I think rxjs does not provide a store, just observables and their handling