mirror of
https://github.com/zhigang1992/angular.js.git
synced 2026-04-01 08:56:08 +08:00
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
This commit is contained in:
committed by
Martin Staffa
parent
bb16759f0b
commit
e8941c0fe5
@@ -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();
|
||||
};
|
||||
|
||||
@@ -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() {
|
||||
|
||||
Reference in New Issue
Block a user