【问题标题】:$debounce encounters an error$debounce 遇到错误
【发布时间】:2014-01-26 10:35:23
【问题描述】:

在我的 Controller 中注入 $debounce 时,我遇到了这个错误:Unknown provider: $debounceProvider

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;
       };
}]);

【问题讨论】:

标签: angularjs debouncing


【解决方案1】:

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

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

因为实现是:

使用 ng-model-options 在 Angular 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(); 
      });
    }
  }

参考文献

【讨论】:

    猜你喜欢
    • 2017-10-11
    • 2015-11-09
    • 1970-01-01
    • 1970-01-01
    • 2021-01-16
    • 1970-01-01
    • 2015-03-12
    • 2020-08-16
    • 2018-06-09
    相关资源
    最近更新 更多