Prevent input from setting form $dirty angularjs

前端 未结 7 1424
天涯浪人
天涯浪人 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:33

    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:

    
    

提交回复
热议问题