On one of the pages in our AngularJS 1.* application, I am looking at a text input1 whose debounce
is set to a value greater than zero. It is configured roughly like this:
<input type="text" data-ng-model="data.value1"
ng-model-options="{ updateOn: 'default blur', debounce: { default: 2500, blur: 0 }}"
data-ng-change="onValueChanged()">
The data entered via that text input is processed when clicking a button, or when hitting the Enter key.
Now, the issue is that when the user hits Enter immediately after changing the value, the new value is not yet in the model due to the debounce setting.
The only hint towards a solution that I could find talks about using ngSubmit
instead of ngClick
, but we are neither using a form element nor ngClick
(the latter at least not where it matters). The Enter keypress is captured in a custom directive via a keydown
event.
Can I somehow force AngularJS to skip the debounce period and immediately update the model on my command in the handler of that keydown
event?
Note that an obvious workaround would be to use a $timeout
slightly higher than the debounce delay in the keydown
handler, so as to be sure that the model has been updated. That feels really hacky to me, though, and also conflicts with the user expectation to get some immediate reaction when hitting the Enter key.
1: In fact, it's a dynamically generated UI that may contain any number of such text inputs in nested containers.