Angular 1.2: Is it possible to exclude an input on form dirty checking?

前端 未结 2 1846
不思量自难忘°
不思量自难忘° 2021-02-20 00:19

In the example beneath, is it possible to ignore the dirty state of the dropdown list? Now it get\'s dirty if the user changes the selected person. But I don\'t care if this fie

2条回答
  •  孤城傲影
    2021-02-20 00:54

    boindiil's directive based solution works but has a flaw: it stops working if form's $setPritine is executed manually. This can be solved by adding an extra line that wipes out the method behavior for the input:

    angular.module('myApp', []).directive('ignoreDirty', [function() {
        return {
        restrict: 'A',
        require: 'ngModel',
        link: function(scope, elm, attrs, ctrl) {
          ctrl.$setPristine = function() {};
          ctrl.$pristine = false;
        }
      }
    }]);
    

提交回复
热议问题