Phi*_*ler 24 javascript angularjs
似乎无法谷歌举例说明如何做到这一点.
我已经成功创建了一个文本框,每次更改时都会调用一个函数.我想要做的只是在用户停止输入x毫秒时调用该函数.
我知道如何使用keyup事件在JQuery中执行它,并且可能使它以这种方式工作,但是想要"Angular Way".
编辑
也许从标签或文本中不清楚,但我使用的是AngularJS,并希望使用正确的方法来创建这种延迟功能.
Rez*_*eza 26
有ng-model-options这种方式
<input id="delayedModel" ng-model="delayedModel" ng-change="callDelayed()" ng-model-options="{ updateOn: 'default blur', debounce: {'default': 500, 'blur': 0} }" />
Run Code Online (Sandbox Code Playgroud)
该callDelayed方法仅在键入最后一个字符或模糊后500毫秒后调用
这是文档https://docs.angularjs.org/api/ng/directive/ngModelOptions
cha*_*tfl 23
对于角度方法,可以$timeout在控制器中注入依赖关系并使用$watch您指定的范围变量ng-model.
var timer=false;
$scope.ModelName='foo';
$scope.$watch('ModelName', function(){
if(timer){
$timeout.cancel(timer)
}
timer= $timeout(function(){
/* run code*/
},delay)
});
Run Code Online (Sandbox Code Playgroud)