I have applied ng-model-options on input with following configuration
ng-model-options="{updateOn:'default blur',debounce:{default:1000,blur:0}}"
And as per the applied configuration to the ng-model-options I am expecting the updated ng-model's value on ng-blur event but it doesn't return the new value despite having set 0 debounce value for blur event.
*Note: This problem is occurred only if user focused out before the time given in the default debounce i.e. 1000
HTML:
<input type="text" ng-model="myname" ng-blur="onBlur(myname)" ng-model-options="{updateOn:'default blur',debounce:{default:1000,blur:0}}">
<input type="text" ng-model="output"/>
JS:
$scope.myname = "Yogesh";
$scope.output = "";
$scope.onBlur = function(a){
$scope.output = a;
}
Plunker link: https://embed.plnkr.co/XJMUUD/
Why debounce is not working? correct me if I am doing wrong any!
Thanks in advance :)
I also have given the answer to my question! let me know how it is flexible to use and how it will help to reduce event digest cycles.
This is because when you set
debouncethe digest loop is triggered after the given time. After the digest loop is triggered It checks whether a value has changed that hasn’t yet been synchronized across the app.In your case the input value will be synchronized with the model variable
mynameafter 1000ms or 1s but immediate update when removing the focus. Your methodonBlur(myname)is called with the previous value ofmyname, because at the time function was called it still has the previous value of the argument passed to it (it can't update the value of myname and call the function at same time) and after that the digest loop updatemyname. You may check that the model is update immediatly by putting{{myname}}next to the inputs.To update the model before your element lose focus (onblur) you have to use
updateOn: changeand set its time to 0, that's how on each change angular will immediatly bind the new value to your function param.