Angular 1.2:是否可以在表格脏检查中排除输入?

Hof*_*ffZ 12 forms validation angularjs

在下面的示例中,是否可以忽略下拉列表的脏状态?现在,如果用户更改所选人员,则会变脏.但是我不关心这个字段在我的表单验证中是否很脏.

function TestingCtrl($scope) {
  $scope.company = '';
  $scope.persons = [{
    name: 'Alice'
  }, {
    name: 'Bob'
  }];


  $scope.selectedPerson = $scope.persons[0];

  $scope.checkForm = function() {
    if ($scope.personForm.$dirty) {
      alert('Form is dirty');
    } else {
      alert('Form is clean');
    }
  }

}
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.7/angular.min.js"></script>


<div ng-app>

  <div ng-controller="TestingCtrl">

    <form name="personForm" novalidate>
      Company:
      <input type="text" ng-model="company" required>
      <br>Persons:
      <select ng-options="p.name for p in persons" ng-model="selectedPerson"></select>

    </form>

    <br>
    <button ng-click="checkForm()">Check if dirty</button>

  </div>

</div>
Run Code Online (Sandbox Code Playgroud)

Kon*_*man 19

boindiil的基于指令的解决方案有效,但有一个缺陷:如果$setPritine手动执行表单,它将停止工作.这可以通过添加额外的行来解决,该行消除了输入的方法行为:

angular.module('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)


boi*_*iil 17

我不知道你的表单中是否有任何其他输入元素.但在您的情况下,您可以明确地检查公司输入是否是脏的:

function TestingCtrl($scope) {
  $scope.company = '';
  $scope.persons = [{
    name: 'Alice'
  }, {
    name: 'Bob'
  }];


  $scope.selectedPerson = $scope.persons[0];

  $scope.checkForm = function() {
    var isDirty = false;

    angular.forEach($scope.personForm, function (value, key) {
        // Input element
        if (value.hasOwnProperty('$modelValue') && value.$name!='person') {
            isDirty = isDirty || value.$dirty;
        }
    });
    if (isDirty ) {
      alert('Form is dirty');
    } else {
      alert('Form is clean');
    }
  }

}
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.7/angular.min.js"></script>


<div ng-app>

  <div ng-controller="TestingCtrl">

    <form name="personForm" novalidate>
      Company:
      <input name="company" type="text" ng-model="company" required>
      <br>Persons:
      <select name="person" ng-options="p.name for p in persons" ng-model="selectedPerson"></select>

    </form>

    <br>
    <button ng-click="checkForm()">Check if dirty</button>

  </div>

</div>
Run Code Online (Sandbox Code Playgroud)

更新 我已更新我的解决方案,现在您可以排除特定的输入字段.但是,每个输入字段都必须设置属性名称

更新II

更清晰的解决方案是使用一个指令,如果设置了特定输入的值,该指令可防止表单将状态设置为脏.这里有一个例子:

angular.module('myApp', []).directive('ignoreDirty', [function() {
    return {
    restrict: 'A',
    require: 'ngModel',
    link: function(scope, elm, attrs, ctrl) {
      ctrl.$pristine = false;
    }
  }
}]);
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
  <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)

  • 好主意,但在我的真实应用程序中,我有很多输入字段,所以这不是一个好的解决方案. (2认同)
  • 我的方法有效,因为如果值已更改且当前状态为原始状态,则仅设置脏标志.通过将pristine的初始值设置为false,永远不会设置脏标志.看一下angularjs代码的`$ commitViewValue`函数 (2认同)