element.bind('focus', function () {})>For some reason focus doesn't seem to work at all - I can not get the break point to execute when I open Developer's tools in debugger.
> element[0].focus(function () { > if (form) > formDirtyState = form.$dirty; // save form's dirty state > }); > > element.bind('blur', function () { > if (currentControl) { > currentControl.$dirty = false; // Remove dirty state but keep the value > if (!formDirtyState && form) > form.$setPristine(); > > // scope.$apply(); > } > });>
>>>>>(function () { >>>>> 'use strict'; >>>>> >>>>> var app = angular.module('sysMgrApp'); >>>>> >>>>> app.directive('noDirtyCheck', [function () { >>>>> // Interacting with input elements having this directive won't cause the >>>>> // form to be marked dirty. >>>>> // http://stackoverflow.com/questions/17089090/prevent-input-from-setting-form-dirty-angularjs >>>>> return { >>>>> restrict: 'A', >>>>> require: ['^form', '^ngModel'], >>>>> >>>>> link: function (scope, element, attrs, controllers) { >>>>> var form = controllers[0]; >>>>> >>>>> var currentControl = controllers[1]; >>>>> >>>>> var formDirtyState = false; >>>>> >>>>> element[0].focus(function () { >>>>> if (form) >>>>> formDirtyState = form.$dirty; // save form's dirty state >>>>> }); >>>>> >>>>> element.bind('blur', function () { >>>>> if (currentControl) { >>>>> currentControl.$dirty = false; // Remove dirty state but keep the value >>>>> if (!formDirtyState && form) >>>>> form.$setPristine(); >>>>> >>>>> // scope.$apply(); >>>>> } >>>>> }) >>>>> } >>>>> }; >>>>> }]); >>>>>})();>>>>>
var dirty = false; >>>>>>>>angular.forEach(scope.theForm, function (value, key) { >>>>>>>> if (key[0] != '$') { >>>>>>>> if (value.$dirty) { >>>>>>>> dirty = true; >>>>>>>> } >>>>>>>> } >>>>>>>>}); >>>>>>>>if (!dirty) { >>>>>>>>scope.theForm.$setPristine(); >>>>>>>>}This will reset the form if all inputs are pristine.....
>>>>>>>require: ['^form'],>>>>>>>
var dirty = false; >>>>>> var theForm = null; >>>>>> angular.forEach(scope.theForm, function (value, key) { >>>>>> if (key == '$$parentForm') { >>>>>> theForm = value; >>>>>> } >>>>>> if (key[0] != '$') { >>>>>> if (value.$dirty) { >>>>>> dirty = true; >>>>>> } >>>>>> } >>>>>> }); >>>>>> if (!dirty) { >>>>>> theForm.$setPristine(); >>>>>> }(Uses the control to reach up to the parent form)