0

在我的控制器中注入 $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;
       };
}]);
4

1 回答 1

0

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

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

因为实现是:

使用 ng-model-options 在 Angular 1.3 中原生支持 debounce

来源如下:

$$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(); 
      });
    }
  }

参考

于 2015-08-25T19:48:26.503 回答