-
Notifications
You must be signed in to change notification settings - Fork 27.5k
fix(ngModel): use $evalAsync
for blur events to safely $apply
#9808
Conversation
If the model is blurred during an apply it should trigger the $touched asynchronously. Fixes angular#8762
scope.$apply(function() { | ||
modelCtrl.$setTouched(); | ||
}); | ||
if (scope.$$phase) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should check $rootScope.$phase
, like here https://github.com/angular/angular.js/blob/master/src/ng/directive/ngEventDirs.js#L62
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Narretz means $rootScope.$$phase
(just clearing it up to avoid confusion 😈).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good I'll try to update that. I followed 719c747#diff-eefe7d65b6795cba34d2b08a27ebf2beR63 which only checked scope.$$phase
.
Just to help my understanding is there a reason $rootScope.$$phase
isn't used there? @tbosch thanks!
Ahh, sorry I see now, it came afterwards I should have checked the most recent version. Thanks for the tips guys.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the clarification @gkalpak Why $rootScope is explained here: #8849 (comment)
38f8282
to
cd435ca
Compare
@Narretz thanks for all your help with this one. Do you think it also makes sense to update the example in the docs at https://github.com/angular/angular.js/blob/master/src/ng/directive/input.js#L1645 with a check of the |
On second thought, if you think it is useful I might open a different PR as a doc fix as I might not have as much time in the upcoming evenings. |
Please so! We can merge doc fixes faster anyway ----- Ursprüngliche Nachricht ----- On second thought, if you think it is useful I might open a different PR as a doc fix as I might not have as much time in the upcoming evenings. |
I don't think we should promote the use of private, undocumented APIs (such as |
That's a good point, I wonder instead if the solution would be to have a public safe apply function that internally checks the phase and does the right thing accordingly.
|
That's true, I thought the example already used |
Why not just use |
Anyway, this looks good to me now |
@Narretz: Not sure if it was clear, but I meant why not use |
Oh, okay. Yeah, I wanted to ask if you meant that, but then I didn't somehow :) |
This refactors the date parsing to allow it to be used in the ui-date directive. Breaking Change the blur event isn't fired. This can be added back when angular/angular.js#9808 is merged
This refactors the date parsing to allow it to be used in the ui-date directive. Breaking Change the blur event isn't fired. This can be added back when angular/angular.js#9808 is merged
var control = $rootScope.myForm.myControl; | ||
|
||
$rootScope.$apply(function() { | ||
expect(control.$touched).toBe(false); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a specifc reason why the first two expects are inside the $apply block?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Narretz there is no specific reason for the two expects to be in there. Do you think it would be better to move them just before the apply block? I was just trying to ensure that the touched and untouched state remained exactly the same before and after the blur event.
Set the
$touched
asynchronously using$evalAsync
if the element is blurred during an$apply
, otherwise it keeps the old behaviour.Fixes #8762