【问题标题】:angular conditional watch to call a debounced method or not is not working角度条件手表是否调用去抖动方法不起作用
【发布时间】:2014-12-07 12:24:38
【问题描述】:

我有以下角度手表来监视范围变量,我执行以下操作。我使用 debounce 将请求捆绑到后端。

  var myWatch = $scope.$watch('myQuery', _.debounce(loadMyData, 1000),true);

当这个 myQuery 返回 undefined 时,我不需要这个 debounce,而是需要直接执行后端调用。

  var myWatch =  $scope.$watch('myQuery', function(oldquery,newquery){
      if(newquery){
        _.debounce(loadMyData(newquery), 2000,true);
      }else{
        loadMyData(newquery);
      }
  },true);

现在去抖动没有正确捆绑我的请求。

【问题讨论】:

    标签: javascript angularjs underscore.js debouncing


    【解决方案1】:

    你需要在一个变量中存储一个新函数,并在监听监听器中调用它:

    var debouncedLoadMyData = _.debounce(loadMyData, 1000, true),
        myWatch = $scope.$watch('myQuery', function (oldquery, newquery) {
            if (newquery) {
                debouncedLoadMyData(newquery);
            } else {
                loadMyData(newquery);
            }
        }, true)
    ;
    

    【讨论】:

      猜你喜欢
      • 2017-12-17
      • 2013-05-17
      • 2019-04-18
      • 1970-01-01
      • 1970-01-01
      • 2015-10-19
      • 2020-04-26
      • 1970-01-01
      • 2017-07-08
      相关资源
      最近更新 更多