Search Posts

Category: singleton

How to create singleton service for timer in angularjs?

I am doing a project in angularjs (1.x). My requirement is that I need to count the number of seconds elapsed. For that, I have written a code using $interval service in my controller which is working perfectly fine. Now , since this code is shared by many objects, so I thought to make it singleton. How can we do so? Below is my code inside controller: scope.timeDiff = 0; var intervalId = $interval(function(){ scope.timeDiff […]

How to inject service in private constructor

I have an existing code implementing singleton pattern by having private constructor and returning object instance returning the object – export class SingletonFactory { private static factoryInstance = new SingletonFactory(); private constructor() { } public static getInstance() { return SingletonFactory.factoryInstance; } } I need to inject a dependency into this factory. And I changed my code to the following – @Inject(‘MyService’) export class SingletonFactory { private static factoryInstance = new SingletonFactory(); private constructor(private myService : […]