How to put a delay on AngularJS instant search?

前端 未结 13 1458
醉梦人生
醉梦人生 2020-11-30 16:55

I have a performance issue that I can\'t seem to address. I have an instant search but it\'s somewhat laggy, since it starts searching on each keyup().

相关标签:
13条回答
  • 2020-11-30 17:36

    Debounced / throttled model updates for angularjs : http://jsfiddle.net/lgersman/vPsGb/3/

    In your case there is nothing more to do than using the directive in the jsfiddle code like this:

    <input 
        id="searchText" 
        type="search" 
        placeholder="live search..." 
        ng-model="searchText" 
        ng-ampere-debounce
    />
    

    Its basically a small piece of code consisting of a single angular directive named "ng-ampere-debounce" utilizing http://benalman.com/projects/jquery-throttle-debounce-plugin/ which can be attached to any dom element. The directive reorders the attached event handlers so that it can control when to throttle events.

    You can use it for throttling/debouncing * model angular updates * angular event handler ng-[event] * jquery event handlers

    Have a look : http://jsfiddle.net/lgersman/vPsGb/3/

    The directive will be part of the Orangevolt Ampere framework (https://github.com/lgersman/jquery.orangevolt-ampere).

    0 讨论(0)
  • 2020-11-30 17:40

    I think the easiest way here is to preload the json or load it once on$dirty and then the filter search will take care of the rest. This'll save you the extra http calls and its much faster with preloaded data. Memory will hurt, but its worth it.

    0 讨论(0)
  • 2020-11-30 17:47

    Why does everyone wants to use watch? You could also use a function:

    var tempArticleSearchTerm;
    $scope.lookupArticle = function (val) {
        tempArticleSearchTerm = val;
    
        $timeout(function () {
            if (val == tempArticleSearchTerm) {
                //function you want to execute after 250ms, if the value as changed
    
            }
        }, 250);
    }; 
    
    0 讨论(0)
  • 2020-11-30 17:48

    For those who uses keyup/keydown in the HTML markup. This doesn't uses watch.

    JS

    app.controller('SearchCtrl', function ($scope, $http, $timeout) {
      var promise = '';
      $scope.search = function() {
        if(promise){
          $timeout.cancel(promise);
        }
        promise = $timeout(function() {
        //ajax call goes here..
        },2000);
      };
    });
    

    HTML

    <input type="search" autocomplete="off" ng-model="keywords" ng-keyup="search()" placeholder="Search...">
    
    0 讨论(0)
  • 2020-11-30 17:52

    Just for users redirected here:

    As introduced in Angular 1.3 you can use ng-model-options attribute:

    <input 
           id="searchText" 
           type="search" 
           placeholder="live search..." 
           ng-model="searchText"
           ng-model-options="{ debounce: 250 }"
    />
    
    0 讨论(0)
  • 2020-11-30 17:53

    I solved this problem with a directive that basicly what it does is to bind the real ng-model on a special attribute which I watch in the directive, then using a debounce service I update my directive attribute, so the user watch on the variable that he bind to debounce-model instead of ng-model.

    .directive('debounceDelay', function ($compile, $debounce) {
    return {
      replace: false,
      scope: {
        debounceModel: '='
      },
      link: function (scope, element, attr) {
        var delay= attr.debounceDelay;
        var applyFunc = function () {
          scope.debounceModel = scope.model;
        }
        scope.model = scope.debounceModel;
        scope.$watch('model', function(){
          $debounce(applyFunc, delay);
        });
        attr.$set('ngModel', 'model');
        element.removeAttr('debounce-delay'); // so the next $compile won't run it again!
    
       $compile(element)(scope);
      }
    };
    });
    

    Usage:

    <input type="text" debounce-delay="1000" debounce-model="search"></input>
    

    And in the controller :

        $scope.search = "";
        $scope.$watch('search', function (newVal, oldVal) {
          if(newVal === oldVal){
            return;
          }else{ //do something meaningful }
    

    Demo in jsfiddle: http://jsfiddle.net/6K7Kd/37/

    the $debounce service can be found here: http://jsfiddle.net/Warspawn/6K7Kd/

    Inspired by eventuallyBind directive http://jsfiddle.net/fctZH/12/

    0 讨论(0)
提交回复
热议问题