45

I'm a newbie to AngularJs, so this might be trivial. Are there any inbuilt AngularJs directive to detect unsaved data in a form. If not then how to go about writing one. Any pointers would be appreciated.

html code is

<input type="text" runat="server" />

And my angular js controller code is

    function MyCtrl1($scope) {
      // code to do stuff
}MyCtrl1.$inject = ['$scope'];

I am trying to write a directive to detect unsaved data, and I'm guessing its to be written in the above controller.Correct me if wrong.

Muhammad Nasir
  • 2,126
  • 4
  • 35
  • 63
iJade
  • 23,144
  • 56
  • 154
  • 243

4 Answers4

76

AngularJS sets the CSS classes ng-pristine and ng-dirty on any input field you've used ng-model on, and your FormController has the properties $pristine and $dirty which you can check to see if the form is dirty or not. So yes, it's possible.

Could you provide some code that shows what you're trying to do? That would make it easier to help you.

EDIT

Here's a simple example of how to detect a pristine/dirty state, and how to revert to a pristine state:

<!doctype html>
<html ng-app>
<head>
    <script src="http://code.angularjs.org/1.1.2/angular.min.js"></script>
    <script type="text/javascript">
    function Ctrl($scope) {
        var initial = {text: 'initial value'};
        $scope.myModel = angular.copy(initial);
        $scope.revert = function() {
            $scope.myModel = angular.copy(initial);
            $scope.myForm.$setPristine();
        }
    }
    </script>
</head>
<body>
    <form name="myForm" ng-controller="Ctrl">
        myModel.text: <input name="input" ng-model="myModel.text">
        <p>myModel.text = {{myModel.text}}</p>
        <p>$pristine = {{myForm.$pristine}}</p>
        <p>$dirty = {{myForm.$dirty}}</p>
        <button ng-click="revert()">Set pristine</button>
    </form>
</body>
</html>
Anders Ekdahl
  • 22,685
  • 4
  • 70
  • 59
  • Dat works fine.I know its another question but is it possible to notify user of unsaved changes when he tries to close the browser or navigate to some other page – iJade Feb 13 '13 at 10:36
  • Sure, that's absolutely possible. You'd do it with a directive on the form, but like you said, that should be in another question here. – Anders Ekdahl Feb 13 '13 at 11:39
  • posted that as another question.Have a look. http://stackoverflow.com/questions/14852802/detect-unsaved-changes-and-alert-user-using-angularjs – iJade Feb 13 '13 at 11:45
  • 1
    It seems that $setPristine has been removed in the latest version of Angular. – sthomps Apr 27 '13 at 22:04
  • 19
    $dirty/$pristine don't seem to work as expected, at least in 1.0.7. If I have a simple form and change a value, $dirty is set to true. However, if I change the value back to the initial value, $dirty remains true. Coming from Knockout, there's a way to specify how to tell if your model is dirty (the default is it converts the entire model to JSON and does a string compare). Is that possible in Angular? – Corey Cole Jun 08 '13 at 09:30
  • `$setPrestine` still exists in angular inside [`NgModelController`](https://docs.angularjs.org/api/ng/type/ngModel.NgModelController) – svarog Sep 10 '17 at 09:35
35

Monitoring pristine/dirty state is a good place to start, but if you want to provide user with the best possible usability, you will have to compare current form data with initial form data to detect any changes. If form is dirty it still doesn't mean that it has changed data.

I've created a very small and useful module to solve this exact problem. With it you can keep your controller code as simple as possible. It adds modified property to every model and even form controller automatically and you can reset entire form by just calling a provided reset() method, so you can concentrate on your application's business logic instead of detecting changes manually.

Please see the Demo.

You can find a distribution package as well as a source code here: https://github.com/betsol/angular-input-modified (it's also available via Bower)

If you will need any help with using this library - you can contact me personally. I will be glad to help. Cheers!

Slava Fomin II
  • 26,865
  • 29
  • 124
  • 202
  • I"ve tried this one. But for me modified yes or no, doesn't change at all. I am using oclazyload to load the modules. I am so frustrated. but wonderful plugin. Do you have any step by step procedure to implement? – Jeeva J Feb 16 '16 at 10:42
  • Modified always false for me :-( – Jeeva J Feb 16 '16 at 10:44
  • This one is really interesting: _"If form is dirty it still doesn't mean that it has changed data."_ Would love to know what that means :) – sakovias Feb 02 '17 at 15:37
  • 2
    @sakovias you could enter "ABC" to the input field and then erase the content. Angular will treat this input as dirty, but underlying data is still the same, i.e. empty string. So it's dirty, but not modified. – Slava Fomin II Feb 02 '17 at 16:25
  • it would make more sense if there was another attribute, `changed`, which does this by default. Sadly, not even Angular 2 or 4.0 includes such functionality. – phil294 Mar 27 '17 at 06:28
16

This is what I did in my Controller.

When I get the form data for modification, first I save its string representation to a scope variable like this:

$scope.originalData = JSON.stringify($scope.data);

Then I create a state change listener:

 var $locationChangeStartUnbind = $scope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) {
    if ($scope.originalData !== JSON.stringify($scope.data)) {
        //Show alert and prevent state change
    } else {
        //DO NOTHING THERE IS NO CHANGES IN THE FORM
    }
});

Then I clear the listener on scope destroy:

$scope.$on('$destroy', function () {
    window.onbeforeunload = null;
    $locationChangeStartUnbind();
});

Hope this helps.

doppelgreener
  • 4,809
  • 10
  • 46
  • 63
manukyanv07
  • 469
  • 6
  • 4
  • 6
    This is excellent, thank you! ps: sometimes Angular may add `$$HashKey` properties to the model; using `angular.toJson($scope.data)` instead of 'JSON.stringify($scope.data)` will mean that angular will take care of removing any tracking variables it's using to track the states. – iamserious Aug 25 '15 at 12:49
  • 1
    This is exactly what I was looking for - thanks! Following up on @iamserious 's comment, I also found it much more reliable to use `angular.toJson($scope.data)` – XstiX Jul 06 '16 at 07:34
  • 1
    Dont use listeners in contollers. Check up any bestpractise – Jens Alenius Sep 22 '16 at 14:06
1

Try this directive that works with ui-router

https://github.com/facultymatt/angular-unsavedChanges

pdorgambide
  • 1,787
  • 19
  • 33
  • I've tried your suggestion. But I got a problem. The pristine is not updated once the form is changed. So if the user enter some texts then somehow clear it, the page still detect unsaved data. – asubanovsky May 28 '15 at 05:19