Skip to content

Instantly share code, notes, and snippets.

@andrelashley
Created April 27, 2017 16:27
Show Gist options
  • Save andrelashley/4432ed9395556e8f46841e67ae9cb21a to your computer and use it in GitHub Desktop.
Save andrelashley/4432ed9395556e8f46841e67ae9cb21a to your computer and use it in GitHub Desktop.
Angular2 routing breaks when navigating in a method call within the subscribe callback of an observable
private submitLoginForm() {
const data = this.formGroup.value;
this.lastSubmission = JSON.stringify(data);
const toast = this.toaster.toast({
title: "Logging in..."
});
this.tellwell.doLogin(data)
.subscribe(result => {
this.toaster.clear(-1, toast);
if (!result.success)
this.handleUnsuccessfulLogin(result.message);
else {
this.lastSubmission = "";
this.unsubscribeAll();
// this.doAfterLoggedIn();
this.router.navigate(["/"]); // this works
}
});
}
private submitLoginForm() {
const data = this.formGroup.value;
this.lastSubmission = JSON.stringify(data);
const toast = this.toaster.toast({
title: "Logging in..."
});
this.service.doLogin(data)
.subscribe(result => {
this.toaster.clear(-1, toast);
if (!result.success)
this.handleUnsuccessfulLogin(result.message);
else {
this.lastSubmission = "";
this.unsubscribeAll();
this.doAfterLoggedIn();
}
});
}
private doAfterLoggedIn() {
let returnRoute = this.route.snapshot.queryParams["ReturnUrl"] || "/";
this.toaster.toast({
title: `Welcome back!`,
duration: 2000
});
this.router.navigate([returnRoute]); // the call that was failing/breaking the router
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment