diff --git a/src/ng/directive/input.js b/src/ng/directive/input.js index 94c2adc0..736b9853 100644 --- a/src/ng/directive/input.js +++ b/src/ng/directive/input.js @@ -1870,6 +1870,25 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$ $animate.addClass($element, PRISTINE_CLASS); }; + /** + * @ngdoc method + * @name ngModel.NgModelController#$setDirty + * + * @description + * Sets the control to its dirty state. + * + * This method can be called to remove the `ng-pristine` class and set the control to its dirty + * state (`ng-dirty` class). A model is considered to be dirty when the control has been changed + * from when first compiled. + */ + this.$setDirty = function() { + ctrl.$dirty = true; + ctrl.$pristine = false; + $animate.removeClass($element, PRISTINE_CLASS); + $animate.addClass($element, DIRTY_CLASS); + parentForm.$setDirty(); + }; + /** * @ngdoc method * @name ngModel.NgModelController#$setUntouched @@ -2139,11 +2158,7 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$ // change to dirty if (ctrl.$pristine) { - ctrl.$dirty = true; - ctrl.$pristine = false; - $animate.removeClass($element, PRISTINE_CLASS); - $animate.addClass($element, DIRTY_CLASS); - parentForm.$setDirty(); + this.$setDirty(); } this.$$parseAndValidate(); }; diff --git a/test/ng/directive/inputSpec.js b/test/ng/directive/inputSpec.js index b157ab9b..ea42034d 100644 --- a/test/ng/directive/inputSpec.js +++ b/test/ng/directive/inputSpec.js @@ -139,6 +139,23 @@ describe('NgModelController', function() { }); }); + describe('setDirty', function() { + + it('should set control to its dirty state', function() { + expect(ctrl.$pristine).toBe(true); + expect(ctrl.$dirty).toBe(false); + + ctrl.$setDirty(); + expect(ctrl.$pristine).toBe(false); + expect(ctrl.$dirty).toBe(true); + }); + + it('should set parent form to its dirty state', function() { + ctrl.$setDirty(); + expect(parentFormCtrl.$setDirty).toHaveBeenCalled(); + }); + }); + describe('setUntouched', function() { it('should set control to its untouched state', function() {