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

Commit e8941c0

Browse files
IShotTheSheriffNarretz
authored andcommitted
feat(ngModelController): add $setDirty method
- extract existing functionality to public method: $setDirty - add tests to corresponding changes - refactor code to use extracted method Closes #10038 Closes #10049
1 parent bb16759 commit e8941c0

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

src/ng/directive/input.js

+20-5
Original file line numberDiff line numberDiff line change
@@ -1870,6 +1870,25 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
18701870
$animate.addClass($element, PRISTINE_CLASS);
18711871
};
18721872

1873+
/**
1874+
* @ngdoc method
1875+
* @name ngModel.NgModelController#$setDirty
1876+
*
1877+
* @description
1878+
* Sets the control to its dirty state.
1879+
*
1880+
* This method can be called to remove the `ng-pristine` class and set the control to its dirty
1881+
* state (`ng-dirty` class). A model is considered to be dirty when the control has been changed
1882+
* from when first compiled.
1883+
*/
1884+
this.$setDirty = function() {
1885+
ctrl.$dirty = true;
1886+
ctrl.$pristine = false;
1887+
$animate.removeClass($element, PRISTINE_CLASS);
1888+
$animate.addClass($element, DIRTY_CLASS);
1889+
parentForm.$setDirty();
1890+
};
1891+
18731892
/**
18741893
* @ngdoc method
18751894
* @name ngModel.NgModelController#$setUntouched
@@ -2139,11 +2158,7 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
21392158

21402159
// change to dirty
21412160
if (ctrl.$pristine) {
2142-
ctrl.$dirty = true;
2143-
ctrl.$pristine = false;
2144-
$animate.removeClass($element, PRISTINE_CLASS);
2145-
$animate.addClass($element, DIRTY_CLASS);
2146-
parentForm.$setDirty();
2161+
this.$setDirty();
21472162
}
21482163
this.$$parseAndValidate();
21492164
};

test/ng/directive/inputSpec.js

+17
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,23 @@ describe('NgModelController', function() {
139139
});
140140
});
141141

142+
describe('setDirty', function() {
143+
144+
it('should set control to its dirty state', function() {
145+
expect(ctrl.$pristine).toBe(true);
146+
expect(ctrl.$dirty).toBe(false);
147+
148+
ctrl.$setDirty();
149+
expect(ctrl.$pristine).toBe(false);
150+
expect(ctrl.$dirty).toBe(true);
151+
});
152+
153+
it('should set parent form to its dirty state', function() {
154+
ctrl.$setDirty();
155+
expect(parentFormCtrl.$setDirty).toHaveBeenCalled();
156+
});
157+
});
158+
142159
describe('setUntouched', function() {
143160

144161
it('should set control to its untouched state', function() {

0 commit comments

Comments
 (0)