rahs
rahs

Reputation: 1899

Subscribing to an observable from within a service vs from within a component

Angular 5

I have a service that contains a method addVal() that makes an HTTP call using an observable.

There is a component from which the addVal() method is being called. Is there any difference (functionality-wise as well as design-wise) in calling the subscribe() method in the component versus in the service?

Upvotes: 1

Views: 794

Answers (1)

mutantkeyboard
mutantkeyboard

Reputation: 1724

My rule of thumb is that I always write services as a thin layer.

They usually contain only a dependency injection for HttpClient and HttpHeaders, and I create just calls to the API endpoints there.

Subscription and data is usually handled by components, since you might want to use different parameters while calling your API.

For that reason alone, it's better to have a slimmer service, and 'fatter' component that does all the logic relevant to itself.

Upvotes: 3

Related Questions