I've implemented a custom input directive - counter
with a reset capability. The directive has require: "ngModel"
.
I am resetting the pristine state of the directive's ngModel
with $setPristine()
. Unlike $setDirty()
, $setPristine()
does not touch the $pristine
state of the parent form.
Q: How do I "notify" the parent form that this directive is no longer "dirty", such that the parent form could have its $pristine
state reset?
Bear in mind that just calling form.$setPristine()
is not enough as there may be other "dirty" controls in the form, which my directive wouldn't (and shouldn't) know about.
This is the directive's link function:
link: function(scope, element, attrs, ngModel){
var original;
ngModel.$render = function(){
original = scope.counter = ngModel.$viewValue;
};
scope.up = function(){
ngModel.$setViewValue(++scope.counter);
};
scope.reset = function(){
scope.counter = original;
ngModel.$setViewValue(scope.counter);
ngModel.$setPristine(); // this sets $pristine on the directive, but not the form
};
}
And here's how it is used:
<div ng-form="form">
<counter ng-model="count"></counter>
</div>