Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Notify user about unsaved data when leaving a component using the Angular Router


    Sometimes we want to be able to prevent the user from navigating away from a given screen. Especially if some data has been modified but not yet saved. The CanDeactivate guard allows us to do exactly this. In this lesson we will take a closer look by implementing one by ourselves.



    Become a Member to view code

    You must be a Pro Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    orLog In




    Instructor: In this application here, I have a list of people. Whenever I click here, a form opens, which allows me to modify that person and to save it again. Now, I've also implemented here some mechanism that visualizes me whenever I have unsafe changes.

    This is implemented very, very naively and very simple. I have here basically the form. Here, you can see that this dirty flag is being visualized, and this is being calculated by simply registering on the value changes for that name field, which represents here for that field of the name of the person.

    Whenever that changes that, we verify whether that name is different of the current person name. If it is, we display here that is dirty true, otherwise dirty is false. Also, if I change here, the dirty flag gets set to true, but then I save it, which is down here.

    Basically by submitting the form, and then I manually reset dirty to false again. Now usually, in a real-world application what you have is that whenever you change this here, and then you have unsafe changes basically, you now navigate to some other view.

    You should actually be notified, telling you about the potential data loss because you didn't save the changes before leaving. The Angular router allows us to implement so-called canDeactivate guard. Let me create here a new class, canDeactivate.guard.ts.

    Let me just paste in here some predefined template. This is a normal Angular service. I implement here the canDeactivate, and the canDeactivate usually takes an instance of a component. Now, we could generalize the part of having some kind of dirty component concept as an interface, and implement it here.

    However, in this specific example here, I'm implementing directly the person detail component. Let's also import that. Then we have to implement the canDeactivate function. The canDeactivate function takes first of all the component, which in this case is a person detail component. 

    Then we have the current route, which is an activated route snapshot, and the current state of the router, as well as the next state. This here returns a Boolean, an observable of a Boolean read, or even a promise. Right now, we just need the Boolean, so let's implement it in that way.

    Now we have the component that gets passed in here to the component instance, we can directly query it. Let's first of all determine whether it is dirty, and we can directly say component.isDirty, so we basically access here that property of our component. If it is dirty, we want to actually inform the user.

    In the case, we do simply a return confirm, "You have unsafe changes." This will return then a Boolean value so we can directly return it. Otherwise, we can simply leave that form, so just return true, because we don't have any dirty changes. Now, obviously, we need to register that canDeactivate dirty component guard here.

    We directly go to the people module. We have here the providers, and we register it there. Then we have to also go into the people routing module, and here on the people detail component, on the person detail component, we specify the canDeactivate property.

    Now, let's navigate to that people module. Let's go here to some person. Let's modify that such that we have unsafe changes. Let's click on contacts.

    Now, you can see you have unsafe changes, do you want to proceed? Yes, I want to proceed anyways. It will let me proceed out of the people detail component here. However, if, for instance, I have unsafe changes, I want to navigate the way in, and click cancel, I remain inside here. I can save those changes, and then I can navigate away without any problems.