Shared service in other provider


I created a shared service to use in all my components/pages.
In this service I do things like testing the network connection etc.
Now I would like to access this functionality in another “regular” provider. For example to test the network to decide where to get my data from.
Basically it works fine, but it creates a new instance of the service instead of using the global one like the components do.

This is how my shared service looks like:

import { Injectable } from '@angular/core';

export class SharedService {
  public hasNetworkConnection : boolean;

  constructor() {  }

  hasNetwork() : boolean {
     /* ... */

And my other provider looks like this:

import { Injectable } from '@angular/core';
import { SharedService } from './sharedservice';

export class ExampleProvider {
  constructor( private sharedService: SharedService ) {  }

  getData() {

Is it even possible to use it in another provider?
Or is there any workaround for this?

1 Like

hi, i am struggling with the same issue :confused:

This stackoverflow question from september seems to be about the same:

unfortunately it has not been answered as well :unamused:


Ok I went over to just try different thing coming to my mind and luckily found what seems to be the cause of the problem :smiley:

In my app.components.ts I had my SharedService added to the providers (like in every tutorial):

  templateUrl: 'app.html',
  providers: [SharedService]

Now I just removed the SharedService from the providers and it stopped creating a new Instance when it’s called inside another provider.
Sometimes you just have to get lucky :wink:

1 Like
1 Like