Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

feat(ngChange): ensure ng-change listeners are run after validation #14043

Open
@NicBright

Description

@NicBright

Hi there,

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:

  1. Let's have a look at the ng-change directive, it utilizes the $viewChangeListeners:
    image
  2. Right now, the $viewChangeListeners are only run inside $$writeModelToScope:
    image
  3. $$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:

image

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions