angular.js icon indicating copy to clipboard operation
angular.js copied to clipboard

ngModelOptions in conjunction with ngBlur

Open iamjohnlong opened this issue 12 years ago • 4 comments

When setting ng-model-options="{updateOn:'blur'}" and calling a method on ng-blur="myUpdateMethod()" the model doesn't get updated in time before the method is called. Not sure if this expected behavior?

Example: http://plnkr.co/edit/4Bhp2wbCwAlC8WN7IFCm?p=preview

iamjohnlong avatar Oct 08 '14 18:10 iamjohnlong

Hmm, not sure what the ideal behavior would be.

Expected behavior aside, I think this is perhaps a needlessly frail way to organize code. If you want one thing to happen after another, I'd recommend using $watch to enforce that constraint.

btford avatar Oct 08 '14 18:10 btford

Another option would be to replace ng-blur with ng-change in this example. This will do exactly what you expected if I understand correctly...

http://plnkr.co/edit/hia7XiCGDXqZy41PKTh6?p=preview

shahata avatar Oct 08 '14 19:10 shahata

@shahata yes. I just realized that as well. Thanks for taking a look. I can close the issue. I was more curious to know if this is the expected behavior.

@btford The example I included was meant to show the issue. My real case has a fairly complex form, and having a $watch on each input would bloat my controller.

iamjohnlong avatar Oct 08 '14 19:10 iamjohnlong

Angular does not give guarantees of the order in which event listeners are installed, and therefore the order of those callbacks is also not defined (meaning it could change in any future release without it being a breaking change). @shahata is right, please use ng-change for this usecase.

Closing...

tbosch avatar Oct 09 '14 18:10 tbosch