0

Is actually possible to do something like this?

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

@Injectable()
export class UbiSharedService {

    private _ubiData: object = {};

    private $$instance: UbiSharedService;

    constructor() {

    }

    public setData(key: string, data: any) {
        this._ubiData[key] = data;
    }

    public getData(key: string): any {
        return this._ubiData[key];
    }

    public popData(key: string): any {
        return delete this._ubiData[key];
    }

    public getInstance(): UbiSharedService {
        if (!!this.$$instance && this.$$instance instanceof UbiSharedService) {
            //NOOP
        }
        else {
            this.$$instance = new UbiSharedService();
        }

        return this.$$instance;
    }


}
4
  • 6
    What are you trying to achieve? The standard behavior of injecting services injects them as a singleton. The state of a singleton will preserve throughout the lifetime of the application. In the case of an Angular application it will preserve as long as the browser window is open. Commented Jan 18, 2018 at 16:07
  • I'm trying to read/write data from multiple components. But I'm having some issues finding the way of using this methods from the components... Commented Jan 18, 2018 at 16:11
  • 1
    So you are trying to use the service in components and call the methods of this service, e.g. setData or getData? Commented Jan 18, 2018 at 16:14
  • I'd like to call setData from A.component and read the same data from B.component :) Commented Jan 18, 2018 at 16:16

2 Answers 2

3

Get rid of the $$instance and instead inject the service into the components that need it using the constructor.

constructor(private productService: ProductService,
                private productParameterService: ProductParameterService) { }

Then the component can access the service methods using the productService or productParameterService properties.

Sign up to request clarification or add additional context in comments.

Comments

1

Please see the following documentation from Angular to learn how to use services to share data between unrelated components: https://angular.io/guide/singleton-services.

You can inject services in components by adding them to the constructor of the component:

constructor(
    private ubiSharedService: UbiSharedService
) { 
   // You can now call in the methods of ubiSharedService
   // e.g.: this.ubiSharedService.setData('key', { value: 'Value' });
}

3 Comments

This is my same suggestion ... just with a better example showing your particular scenario.
Yes I understood that. In fact I'll pick your answer since you answered first :)
@DeborahK please update your answer for others that come here. Include the link to the singleton services angular documentation page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.