Prevent input from setting form $dirty angularjs

前端 未结 7 1439
天涯浪人
天涯浪人 2021-02-07 05:37

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 na

7条回答
  •  臣服心动
    2021-02-07 06:29

    A variation on @overthink's answer with some additional validation, and inline bracket notation to protect against minification.

    "use strict";
    
    angular.module("lantern").directive("noDirtyCheck", [function () {
        return {
            restrict: "A",
            require: "ngModel",
            link: function (scope, elem, attrs, ngModelCtrl) {
                if (!ngModelCtrl) {
                    return;
                }
    
                var clean = (ngModelCtrl.$pristine && !ngModelCtrl.$dirty);
    
                if (clean) {
                    ngModelCtrl.$pristine = false;
                    ngModelCtrl.$dirty = true;
                }
            }
        };
    }]);
    

提交回复
热议问题