Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent input from setting form $dirty angularjs

Tags:

angularjs

I have an ng form on a page. Inside the form I have several controls which need to display a save dialog when the form is dirty, ie form.$dirty = true. However there are some navigation controls in the form I don't want to dirty the form. Assume I can't move the control out of the form.

see: http://plnkr.co/edit/bfig4B

How do I make the select box not dirty the form?

like image 978
acacia Avatar asked Jun 13 '13 14:06

acacia


4 Answers

Here's a version of @acacia's answer using a directive and not using $timeout. This will keep your controllers cleaner.

.directive('noDirtyCheck', function() {
  // Interacting with input elements having this directive won't cause the
  // form to be marked dirty.
  return {
    restrict: 'A',
    require: 'ngModel',
    link: function(scope, elm, attrs, ctrl) {
      ctrl.$pristine = false;
    }
  }
});

Then use it in your form like so:

<input type="text" name="foo" ng-model="x.foo" no-dirty-check>
like image 114
overthink Avatar answered Nov 15 '22 20:11

overthink


I used @overthink's solution, but ran into the problem mentioned by @dmitankin. However, I didn't want to attach a handler to the focus event. So instead, I endeavored to override the $pristine property itself and force it to return false always. I ended up using Object.defineProperty which is not supported in IE8 and below. There are workarounds to do this in those legacy browsers, but I didn't need them, so they are not part of my solution below:

(function () {
    angular
        .module("myapp")
        .directive("noDirtyCheck", noDirtyCheck);

    function noDirtyCheck() {
        return {
            restrict: 'A',
            require: 'ngModel',
            link: function (scope, elem, attrs, ctrl) {
                var alwaysFalse = {
                    get: function () { return false; },
                    set: function () { }
                };
                Object.defineProperty(ctrl, '$pristine', alwaysFalse);
                Object.defineProperty(ctrl, '$dirty', alwaysFalse);
            }
        };
    }
})();

I am also overriding $dirty so it can't be set as dirty either.

like image 41
Kasey Speakman Avatar answered Nov 15 '22 22:11

Kasey Speakman


Setting the $pristine property to false, only when initializing, works until you call $setPristine() on the form. Then your control has its $pristine back to true and changing the input's value would make your form dirty. To avoid that, set the $pristine on focus:

link: function(scope, elm, attrs, ctrl) {
    elm.focus(function () {
        ctrl.$pristine = false;
    });
}
like image 30
Delian Mitankin Avatar answered Nov 15 '22 22:11

Delian Mitankin


Angular only sets the form dirty if the control is pristine. So the trick here is to set $pristine on the control to false. You can do it in a timeout in the controller.

see: http://plnkr.co/edit/by3qTM

like image 28
acacia Avatar answered Nov 15 '22 21:11

acacia