1

In Angular 2, is there a better way to control browser back. Ex: when click on back, show confirm dialog box, if confirmed redirect to page X, otherwise stay in same page.

Using location.onPopState() we cannot have a complete control on browser back as it will eventually do the back redirect.

Can above behavior be achieved using authguard, canActivate of angular2 routing?

1 Answer 1

1

WHat you are looking for is the canDeactivate guard. Victor describes it like this in his book:

The canDeactivate guard is different from the rest. Its main purpose is not to check permissions, but to ask for confirmation. To illustrate this let’s change the application to ask for confirmation when the user closes the compose dialog with unsaved changes.

He then goes on to share how to do a SaveChanges guard:

   canDeactivate(component: ComposeCmp, route: ActivatedRouteSnapshot,
   state: RouterStateSnapshot): Promise<boolean> {
      if (component.unsavedChanges) {
        return this.dialogs.unsavedChangesConfirmationDialog();
      } else {
        return Promise.resolve(true);
      }
    }

I highly suggest buying the book. https://leanpub.com/router

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.