Skip to content

Commit 8e594cf

Browse files
committed
Fix scope.$on('submit') order
Don't watch when init This order is better than using return;
1 parent a08264c commit 8e594cf

File tree

3 files changed

+17
-9
lines changed

3 files changed

+17
-9
lines changed

dist/angular-validation.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -417,10 +417,16 @@
417417
scope.$on(ctrl.$name + 'submit', function () {
418418
var value = element[0].value;
419419

420-
if (attrs.validMethod === 'submit') {
420+
checkValidation(scope, element, attrs, ctrl, validation, value);
421421

422+
if (attrs.validMethod === 'submit') {
422423
watch(); // clear previous scope.$watch
423-
watch = scope.$watch('model', function (value) {
424+
watch = scope.$watch('model', function (value, oldValue) {
425+
426+
// don't watch when init
427+
if (value === oldValue) {
428+
return;
429+
}
424430

425431
// scope.$watch will translate '' to undefined
426432
// undefined/null will pass the required submit /^.+/
@@ -432,10 +438,8 @@
432438
checkValidation(scope, element, attrs, ctrl, validation, value);
433439
});
434440

435-
return;
436441
}
437442

438-
checkValidation(scope, element, attrs, ctrl, validation, value);
439443
});
440444

441445
/**

dist/angular-validation.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/directive.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,16 @@
160160
scope.$on(ctrl.$name + 'submit', function () {
161161
var value = element[0].value;
162162

163-
if (attrs.validMethod === 'submit') {
163+
checkValidation(scope, element, attrs, ctrl, validation, value);
164164

165+
if (attrs.validMethod === 'submit') {
165166
watch(); // clear previous scope.$watch
166-
watch = scope.$watch('model', function (value) {
167+
watch = scope.$watch('model', function (value, oldValue) {
168+
169+
// don't watch when init
170+
if (value === oldValue) {
171+
return;
172+
}
167173

168174
// scope.$watch will translate '' to undefined
169175
// undefined/null will pass the required submit /^.+/
@@ -175,10 +181,8 @@
175181
checkValidation(scope, element, attrs, ctrl, validation, value);
176182
});
177183

178-
return;
179184
}
180185

181-
checkValidation(scope, element, attrs, ctrl, validation, value);
182186
});
183187

184188
/**

0 commit comments

Comments
 (0)