AngularJS 如何在 ng-model-options 中取消抖动

How to cancel debounce in ng-model-options in AngularJS

<form name="editForm">
    Title<input ng-model="task.title" type="text" name=taskTitle ng-model-options="{ updateOn: 'default' ,debounce{'default':2000000} }">
    <a ng-click="UpdateTask(task.title)">SAVE</a>
    <a ng-click="editForm.$rollbackViewValue();">DISCARD</a>
</form>

由于debounce值很长,当我点击"DISCARD"时,ng-model不会反映它的变化。

但是,我想要的是当我点击 "SAVE" 时,我可以更改去抖值或使其过期以便立即反映更改。

我在AngularJs文档中找不到cancelDebounce(),有人愿意提供解决方案吗?谢谢

我建议你有一个作用域变量,它具有像 $scope.myDebounce = 2000000 这样的 debounce 值,然后在控制器中创建一个 discard 函数,它首先将 be-bounce 值重置为0,然后回滚表单更改。在下一个摘要周期中将 myDebounce 变量重置为其实际值。

标记

<form name="editForm">
    Title
    <input ng-model="task.title" 
           type="text" 
           name=taskTitle 
           ng-model-options="{ updateOn: 'default', debounce: {'default': myDebounce } }">
    <a ng-click="UpdateTask(task.title)">SAVE</a>
    <a ng-click="editForm.$rollbackViewValue();">DISCARD</a>
</form>

代码

$scope.myDebounce = 2000000; //somewhere in controller

$scope.discard = function (form) {
   $scope.myDebounce = 0; //resetting debounce to get quick `ng-model` update
   form.$rollbackViewValue(); 
   $timeout(function(){
       //setting the actual debounce value to old one in next digest
       $scope.myDebounce = 2000000; 
   });
}

我认为你真正需要的是 NgModelController.$commitViewValue();

来自angular doc

$commitViewValue() Commit a pending update to the $modelValue. Updates may be pending by a debounced event or because the input is waiting for a some future event defined in ng-model-options. this method is rarely needed as NgModelController usually handles calling this in response to input events.