$ debounce遇到错误

时间:2014-01-08 11:03:48

标签: angularjs debouncing

在我的控制器中注入$ debounce时,我遇到此错误:未知提供商:$ debounceProvider< - $ debounce

myControllers.controller('Controller',
       ['$scope', '$compile', '$rootScope', '$timeout', '$document', '$debounce','promiseTracker',
   function ($scope, $compile, $rootScope, $timeout, $document,$debounce, promiseTracker) {

       $scope.$watch('newquery', function (newValue, oldValue) {
           if (newValue === oldValue) { return; }
           $debounce(applyQuery, 350);
       });
       var applyQuery = function () {
           $scope.filter.query = $scope.query;
       };
}]);

1 个答案:

答案 0 :(得分:0)

本机debounce语法是视图的一部分而不是控制器:

ng-model-options="{ debounce: 1000 }"

因为实施是:

  

使用ng-model-options

在角度1.3中本机支持去抖动

来源如下:

$$debounceViewValueCommit: function(trigger) 
  {
  var debounceDelay = this.$options.getOption('debounce');

  if (isNumber(debounceDelay[trigger]) ) 
    {
    debounceDelay = debounceDelay[trigger];
    } 
  else if (isNumber(debounceDelay['default']) ) 
    {
    debounceDelay = debounceDelay['default'];
    }

  this.$$timeout.cancel(this.$$pendingDebounce);
  var that = this;

  if (debounceDelay) 
    {
    this.$$pendingDebounce = this.$$timeout(function() {
      that.$commitViewValue();
      }, debounceDelay);
    }
  else if (this.$$scope.$root.$$phase) 
    {
    this.$commitViewValue();
    }
  else 
    {
    this.$$scope.$apply(function() {
      that.$commitViewValue(); 
      });
    }
  }

<强>参考