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

问题描述

我有绑定到NG-变化重新查询功能的搜索输入字段。

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?

推荐答案

由于角1.3,这是比较容易的方式来完成,使用的的:

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-变化的延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 20:19