I have developed a TypeScript service that looks something like this:
export default class MyService {
constructor(private $http: ng.IHttpService, ...) {}
method1(data : any) : void {
this.$http(...)
}
method2(data : any) : void {
this.$http(...)
}
}
Now, I also have a controller class that makes use of this service:
export default class MyController {
constructor(private myService: MyService, ...) {}
$onInit(){
let selectedMethod = true ? this.myService.method1 : this.myService.method2;
selectedMethod({ ... data ... });
}
}
The problem arises when the service methods throw an Exception - this is undefined
. If I call the methods directly like
this.myService.method1({ ... data ... })
, it works fine. I could, of course, use selectedMethod.bind(myService)({ ... data ... })
or selectedMethod.call(myService, { ... data ... })
, but why is there a scope difference in the first place?
Thank you.