4

I am blocked with this,
In Angular 2, I need to block any user click event, even browser refresh if the form in the page is dirty

Could you help me with ideas?

Matt
  • 609
  • 2
  • 12
  • 19

2 Answers2

8

It seems you are looking for canDeactivate

It allows you to block routing away. You would need a shared service where you update the forms dirty state so that the guard can access it.

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
5

The angular docs cover this exact question:
https://angular.io/guide/router#candeactivate-handling-unsaved-changes I'd leave that as a comment but I lack the reputation to do so.

Chris Carcel
  • 138
  • 2
  • 6
  • 8
    For those coming to this in 2021 or after, better try this link: https://angular.io/guide/router-tutorial-toh#candeactivate-handling-unsaved-changes – AsGoodAsItGets Mar 22 '21 at 15:11