封装 ng-change 延迟的角度指令 [英] angular directive encapsulating a delay for ng-change

查看:27
本文介绍了封装 ng-change 延迟的角度指令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个搜索输入字段,其中带有绑定到 ng-change 的重新查询函数.

I have a search input field with a requery function bound to the ng-change.

 <input ng-model="search" ng-change="updateSearch()">

然而,这对每个角色都触发得太快了.所以我最终做了很多这样的事情:

However this fires too quickly on every character. So I end up doing something like this alot:

  $scope.updateSearch = function(){
    $timeout.cancel(searchDelay);
    searchDelay = $timeout(function(){
      $scope.requery($scope.search);
    },300);
  }

这样请求只会在用户停止输入 300 毫秒后发出.是否有任何解决方案可以将其包装在指令中?

So that the request is only made 300ms after the user has stopped typing. Is there any solution to wrap this in a directive?

推荐答案

从 angular 1.3 开始,使用 ngModelOptions:

As of angular 1.3 this is way easier to accomplish, using ngModelOptions:

<input ng-model="search" ng-change="updateSearch()" ng-model-options="{debounce:3000}">

Syntax:  {debounce: Miliseconds}

这篇关于封装 ng-change 延迟的角度指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆