Angular 1.2: Is it possible to exclude an input on form dirty checking?
angularjs, forms, validation
Solution
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;
}
}
}]);
Problem
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 field is dirty in my form validation. ``` 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'); } } } ``` ``` <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> ```