Search Posts

Category: dependency-injection

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 : […]

How can I add dependency to the current class using AngularJs and TypeScript?

Let’s say I have a component class as follows; @Component({ selector: “my”, templateUrl: “/app/my.html” }) export class MyComponent extends MyBase { helper: Helper; constructor() { helper = new Helper(this); } } The Helper class lives in another file like this. export class Helper { constructor(protected component: MyBase) { } } It looks like I can’t use dependency injection for my helper class no matter what since it is storing a reference to the component. The […]