This is similar to redirect to a route after api call in angular2 but I want to use a class variable (this.areaCode
) as part of the redirect or update a variable (this.userInfoMessage
). Eg:
onSubmit(form: string): void {
this.taskService.createTask(task).subscribe(this.taskCreated,this.handleError)
}
private taskCreated (resp: any) {
console.log("Task created successfully",resp)
this.router.navigate([`/areas/${this.areaCode}`]); // eg: "/areas/12" //"this" is of type SafeSubscriber, not my class
}
private handleError (error: any) {
this.userInfoMessage="We've got errors here!" //"this" is of type SafeSubscriber, not my class so the message is now bound
console.log(error)
}
However I cannot fathom how to get a reference to the class in these functions. Do I need to use the self=this
trick. If so, how?
Thanks
UPDATE
I have tried passing this
into this.taskService.createTask(task).subscribe(this.taskCreated,this.handleError).bind(this)
but Property 'bind' does not exist on type 'Subscription'.
UPDATE (2)
I guess the question is "how do I pass a reference into a subscription callback?"
Having read the linked Q if I want to subscribe passing in a reference to the current object I have tried using
var self= this
this.taskService.createTask(task).subscribe(this.taskCreated(self),this.handleError)
it does not compile. I still have the callback defined as private taskCreated (resp: any)
I don't think this is quite the same as the linked Q.
What am I missing?
Thanks