AngularJS - 从表单验证/脏中排除字段

Its*_*has 5 javascript forms html5 angularjs

我有一个checkbox内部ng-form说唱HTML5 form,我想排除所以它不会改变我的形式$state($pristine$dirty).

我试过使用ignoreDirty指令 - Angular 1.2:是否可以在表单脏检查中排除输入?

和其他一些解决方案,但没有ng-form内部工作HTML5 form,一些示例代码:

<form name="mainForm" class="col-xs-10 form-validation" novalidate>
  <ng-form name="innerForm">
    <div>
      <span>check my
        <span ng-if="vm.utils.mode!=='readonly'">
          <input type="checkbox" 
                 ng-model="vm.utils.amalotSecond" 
                 class="secondYearCheckBox">
        </span>
      </span>
    </div>
  </ng-form>
<form>
Run Code Online (Sandbox Code Playgroud)

lin*_*lin 6

ignoreDirty你提供的指令在这个演示小提琴中工作得很好.下面的代码示例取自angular-1-2-is-it-possible-to-exclude-an-input-on-form-dirty-checking.还要尽量避免使用不符合HTML的嵌套表单.一个form元素不能有一个元素form.它会导致问题和错误,例如您当前面临的问题和错误.

视图

<div ng-controller="MyCtrl">
  <form name="test">
    Watching dirty: <input ng-model="name"  /><br />
    Ignoring dirty: <select ng-model="gender" ignore-dirty>
      <option>male</option>
      <option>female</option>
    </select><br />
    dirty: {{test.$dirty}}
  </form>
</div>
Run Code Online (Sandbox Code Playgroud)

AngularJS应用程序

var myApp = angular.module('myApp',[]);

myApp.controller('MyCtrl', function ($scope) {
    $scope.name = 'Superhero';
});

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