Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

feat(input): add $setDirty method to the controller of ng-model directive #10049

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions src/ng/directive/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -1849,6 +1849,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 model has been changed
* from when first compiled within then form.
*/
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
Expand Down Expand Up @@ -2081,11 +2100,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();
};
Expand Down
17 changes: 17 additions & 0 deletions test/ng/directive/inputSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down