I have a method handleError() like in the documentation https://angular.io/docs/ts/latest/guide/server-communication.html#!#error-handling
private handleErro
Since you're passing the function directly, you don't have the this
context of your class in there. A really easy and best practice way would be to use a lambda or "fat arrow function":
this.http.get(url, ApiRequest.ACCEPT_JSON)
.map(res => ApiHelper.extractData(res))
.catch(err => this.handleError(err));
A really good read on when to use lambdas: https://stackoverflow.com/a/23045200/1961059
this
in handleError
in your case is probably not what you think it is.
Try to do the following:
this.http.get(url,
ApiRequest.ACCEPT_JSON)
.map(ApiHelper.extractData)
.catch(this.handleError.bind(this)); // <-- add .bind(this)
Possible solution is also to assign your service to static variable of class
ClassToHandleError {
private static loginService: LoginService;
constructor(private loginService: LoginService) {
ClassToHandleError.loginService = loginService;
}
private handleError(error: any) {
console.error(error);
console.log(ClassToHandleError.loginService); // here you can use static reference
return Observable.throw(error);
}
}
I know this is just workaround and rinukkusu provided definetely better solution then me. I used it until I get through this question. But maybe in some special case this will be valuable for somebody :) .