I have a typescript class, in which constructor I have a normal and an angular-injected argument:
export class MyClass {
private translation:string;
public static $inject = ['$filter'];
constructor(name:string, $filter: ng.IFilterService) {
this.translation = filter('translate')('code').toString();
}
}
If I now want to create an object, how can I do it?
new MyClass('myname'); //won't compile because there are too few parameters
new MyClass('myname', filter); //makes no sense since I want to inject it
Even If I wrote $filter? it won't work because it won't recognize the scope and it will be undefined.
So, How can I get this to work?
My Approach
Let's say I am in another class, in which I want to create an object of MyClass. The following code will work but I don't like the idea of having to inject $filter in this class too, since it doesn't need it.
export class ClassUsingTheOtherClass {
private filter:ng.IFilterService;
public static $inject = ['$filter'];
constructor($filter: ng.IFilterService) {
this.filter = $filter;
}
doThings() {
var clazz = new MyClass('myName', this.filter);
}
}
I'd rather call something like this var clazz = new MyClass('myName'); and having $filter automatically injected as dependency in MyClass. Is this possible at all?
new MyClass('myname');? It seems like it would never be called because you need afilterinstance otherwise the class will eventually encounter an undefined error. What is the purpose ofmynameparameter? How would this ever be able to be injected? That definition would not work with Angular.