Search code examples
angularback-button

Angular 2 - Prevent back button while REST API call is under processing?


In my angular 2 application, there is a REST API call has been handled to process some functional flow to get response from server side. On the mean-time, I would like to prevent the user to navigate using Browser Back button.

I have used the below piece of code to prevent back button.

history.pushState(null, null, location.href);
    window.onpopstate = function(event) {
        history.go(1);
        /*** I need to use confirmation box to alert the user ***/
};

I have to alert the user on the window.onpopstate event handler function to handle it. Any clue on creating dialog box...?

Appreciate for any clues..


Solution

  • You can set a property in your service, something like isDisabled = true, and create a directive that you can use anywhere, disabling your button and any other actions you might need.