-
Notifications
You must be signed in to change notification settings - Fork 27.4k
ng-change's viewChangeListener firing before validity update #9564
Comments
I mean the thing is, the click event is just the browser telling us that a click happened, we don't really have a way to tell it to wait until a view change listener is fired (it would be expensive to implement a way to defer evaluating the passed expression until view change, if a view change was needed --- and even if we did, it wouldn't help other jQuery events). I'm not sure there is much we can do about this, but I think it might be wise not to depend on validity state in an event listener unless you're very careful about it. What in particularly are you working on that you need to do this? Maybe there's a better way to take care of it. |
Given what I've said above, and the fact that I have no idea how to triage this one, lets say we close it and you ask around for better solutions to what you're trying to do on Sorry to close the issue but, :( |
I agree that deferred evaluation is not ideal, but as of The long-standing workaround I've used for both pre- and now post- |
It's not actually what's happening with ngEvents (with the exception of focus and blur events). What actually happens for other events is the expression gets evaluated as soon as we get the event. |
Sorry about that - you're absolutely right. Only a few events actually end up getting delayed, and ones such as |
Before
1.3.0-rc.4
, if I had a required, initially empty radio box, state was off a lot (http://jsfiddle.net/butyz12m/):ng-click="change(parentForm)"
, thechange
fn seesparentForm.$invalid
beingtrue
(incorrect) and the model value beingundefined
(incorrect).ng-change="change(parentForm)"
, thechange
fn seesparentForm.$invalid
beingtrue
(incorrect) and the model value beingb
(correct).After
1.3.0-rc.4
, the behavior is now a lot better, but still off slightly in theng-change
case (http://jsfiddle.net/tq321axv/):ng-click="change(parentForm)"
, thechange
fn seesparentForm.$invalid
beingfalse
(correct) and the model value beinga
(correct).ng-change="change(parentForm)"
, thechange
fn seesparentForm.$invalid
beingtrue
(incorrect) and the model value beingb
(correct).From what I could debug/track down,
ng-change
is firing early because it's in the$viewChangeListeners
queue, which occurs before$digest
. I don't know what the appropriate way of delaying the change is, but I'd assume it should stay consistent with whenng-click
and ngEvents fire.I know this is probably under the category of "undefined behavior", but I just wanted to mention it cause I noticed things improving after
1.3.0-rc.4
yet there's still this minor discrepancy betweenng-click
andng-change
. IMO, things have improved a lot due tong-model
being increased in priority: 1064443#diff-c244afd8def7f268b16ee91a0341c4b2R2397.The text was updated successfully, but these errors were encountered: