You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
right now, ng-change listeners do not always run after validation. It's only the case if ngModelOptions.allowInvalid is falsy.
Please let me show you (based on the latest master when writing this post, 9421674) the code-flow to better understand, why and what we can do to change that:
Let's have a look at the ng-change directive, it utilizes the $viewChangeListeners:
Right now, the $viewChangeListeners are only run inside $$writeModelToScope:
$$writeModelToScope is invoked inside $validate which is not a problem, but also inside $$parseAndValidate. In the following Screenshot you'll see the problem. As soon as allowInvalid is truthy, then $$writeModelToScope is invoked before the validators are run. This is problematic, as it should be possible to rely (inside ng-change) on the fact, that the validation state of ngModelCtrl is already up-to-date:
Now, my proposal is, to move the yellow colored code block to come after the validation block.
I think that this change should be relatively trivial if I'm correct that the $$runValidators(...) invocation (line 736) is not dependent on ctrl.$modelValue. As I can't imagine any application relying actually on the contrary (that $viewChangeListeners fire before validation, in case allowInvalid is set to true), I think this would not be a breaking change either.
What do you think?
Regards,
Nicolas
The text was updated successfully, but these errors were encountered:
NicBright
changed the title
feat(ngModel): ensure ng-change listeners are run after validation
feat(ngChange): ensure ng-change listeners are run after validation
Feb 15, 2016
One one hand, it's reasonable, on the other hand you are definitely changing the behavior if you have an asynchronous validator. Because then you definitely have a delay in updating the model / executing ngChange. So this would be a breaking change.
But that the validation state is indetermined when ngChange fires is not good, that must be said.
So I'm undecided if this is worth changing.
You're right. I totally forgot to think about async validators. However, concerning my proposal, moving the yellow colored block would not really delay updating the model, as the async validators run asynchronously.
If we'd move the block inside the callback, it would be like you say:
Maybe there are 2 options:
improve the guarantee about validation state in terms of "all synchronous validators run before ng-change for allowInvalid: true"
make a breaking change and align the behaviour to the standard case ("allowInvalid: false"), where the call to $$writeModelToScope only happens in the doneCallback of $$runValidators, which means that the model update is delayed already, as soon as $asyncValidators are involved.
I am having a similar issue. For me, ngChange is firing prior to validation completing when NgModelOptions allowInvalid is true. Frustrating as I need the allowInvalid to be true for other reasons, however I still need the result of the validation in order to perform other functions.
Hi there,
right now,
ng-change
listeners do not always run after validation. It's only the case ifngModelOptions.allowInvalid
is falsy.Please let me show you (based on the latest master when writing this post, 9421674) the code-flow to better understand, why and what we can do to change that:
ng-change
directive, it utilizes the$viewChangeListeners
:$viewChangeListeners
are only run inside$$writeModelToScope
:$$writeModelToScope
is invoked inside$validate
which is not a problem, but also inside$$parseAndValidate
. In the following Screenshot you'll see the problem. As soon asallowInvalid
is truthy, then$$writeModelToScope
is invoked before the validators are run. This is problematic, as it should be possible to rely (insideng-change
) on the fact, that the validation state of ngModelCtrl is already up-to-date:Now, my proposal is, to move the yellow colored code block to come after the validation block.
I think that this change should be relatively trivial if I'm correct that the
$$runValidators(...)
invocation (line 736) is not dependent onctrl.$modelValue
. As I can't imagine any application relying actually on the contrary (that$viewChangeListeners
fire before validation, in caseallowInvalid
is set totrue
), I think this would not be a breaking change either.What do you think?
Regards,
Nicolas
The text was updated successfully, but these errors were encountered: