-
Notifications
You must be signed in to change notification settings - Fork 0
/
angular-resetform.js
52 lines (27 loc) · 1.2 KB
/
angular-resetform.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
angular.module('angular-resetform', []).directive('resetForm', function ($parse, $compile) {
return {
require: ['^form', 'ngModel'],
link: function (scope, elm, attr, formCtrl) {
var resetFormValue = angular.isDefined(attr.resetFormValue);
var resetFormClear = angular.isDefined(attr.resetFormClear);
formCtrl[0].$resetFormData = function () {
if (resetFormValue) { // to set value on form rest
$parse(attr.ngModel).assign(scope, attr.resetFormValue);
} else {
$parse(attr.ngModel).assign(scope, formCtrl[1].initialValue);
}
};
formCtrl[0].$resetFormComplete = function () {//reset complete form
formCtrl[0].$setPristine();
};
scope.$watch(attr.ngModel, function (value) {
if (formCtrl[1].$pristine) { //if form is touched or not
formCtrl[1].initialValue = value;
if (resetFormClear) { // clear value on reset
formCtrl[1].initialValue = '';
}
}
});
}
}
});