0

Yep, it's a duplicate of this: Angular 4+ ngOnDestroy() in service - destroy observable

This question is maybe violating the terms, since I am asking for advice/opinions/best-practices.

My questions is: Should I have observers and subscriptions in services? I can subscribe in the construcor, but in services there is no onDestroy()-Method to clear my subscriptions.

For example: How do I go about watching a value in a redux-store, which is needed within a service? The service is used by several components. Should the service watch the store? Or should the components watch the store and provide the value in question with every request to the service?

Tobias Gassmann
  • 11,399
  • 15
  • 58
  • 92
  • The components 'watch' the store (using Observables). The service can dispatch an action and the reducers change the store. If you are using Angular 4/5, you can use ngrx/store package to implement redux. – Christian Benseler Nov 23 '17 at 11:19

1 Answers1

1

There is ngOnDestroy in service files. ngOnInit is missing in service files.

Angular 4+ ngOnDestroy() in service - destroy observable [ Refer here ]

Franklin Pious
  • 3,670
  • 3
  • 26
  • 30
  • that's it! I can't believe that I have missed that! ==> "Lifecycle hook that is called when a directive, pipe or service is destroyed." I will accept your answer in 7 minutes (thats the remaining time-limit for accepting answers) – Tobias Gassmann Nov 23 '17 at 11:21
  • I will. Still have to wait 4 minutes for the system to allow accepting answers :-) – Tobias Gassmann Nov 23 '17 at 11:24
  • 1
    Mark the question as a dup, you are basically reusing the answer on the linked question... – Jota.Toledo Nov 23 '17 at 11:24
  • how can I mark it as dup? – Tobias Gassmann Nov 23 '17 at 11:27
  • @TobiasGassmann not sure if you as OP can mark your own question as dup, For a next time, do some more research as just by looking at the OnDestroy documentation (https://angular.io/api/core/OnDestroy) or looking for old questions in SO you would have found your answer :) – Jota.Toledo Nov 23 '17 at 11:37