【问题标题】:I would like to fire a function after the input has been complete我想在输入完成后触发一个函数
【发布时间】:2016-03-10 10:13:02
【问题描述】:

我想在输入完成后触发一个函数。去抖动功能对我不起作用。我不希望它聚焦我希望对按键触发的函数的点击次数更少.

plnkr.co/edit/uDgRNnVh0NUm4z2BbzSj?p=preview 

【问题讨论】:

  • 你能做一个 sn-p 并向我们展示你的尝试吗?
  • 在您的问题中链接 plnkr。您希望何时准确调用该函数?我想我不明白点击次数较少
  • 我希望在收到输入文本后调用该函数。因此它可能会在调用该函数之前等待一秒钟。

标签: javascript angularjs underscore.js


【解决方案1】:

this answer 的帮助下,我得到了它(我希望)你所期望的工作。

http://plnkr.co/edit/f9RaRXjhWZGbTVn0JRno?p=preview

HTML

<!DOCTYPE html>
<html ng-app="app">
    <head>
        <link data-require="bootstrap-css@3.3.6" data-semver="3.3.6" rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.css" />
        <script data-require="angular.js@*" data-semver="1.5.0" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.js"></script>
        <script data-require="underscore.js@*" data-semver="1.8.3" src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-    min.js"></script>
        <script src="script.js"></script>
    </head>
    <body ng-controller="mainCtrl" class="container">
      <div class="panel panel-primary well">
            Input:
            <input type="text" ng-model="inputText" />

        </div>
        {{finalText}}
    </body>
</html>

JS

angular.module('app', []);

angular.module('app').controller('mainCtrl', function($scope) {
 $scope.inputText = "";
 $scope.$watch("inputText", _.debounce(function (text) {
   $scope.$apply(function(){
        $scope.finalText = text;
   });
 }, 1000));
});

【讨论】:

    【解决方案2】:

    在 Angular 世界中,这是使用 ng-blur 完成的。像这样:

    <input type="text" ng-blur="foo()">
    

    【讨论】:

      【解决方案3】:
      var timeout,wait,waitTimeout;
      
      function customDebounce() {
          var dfd = $q.defer(),
           if(!wait) {
          FunctionCall().then(function(response){
            dfd.resolve(response);
          });
          wait = true;
          waitTimeout = $timeout( function(){wait = false;}, 1000);
        }
        else {
          $timeout.cancel(timeout);
          $timeout.cancel(waitTimeout);
          timeout = $timeout( function(){
            wait = false;
            FunctionCall().then(function(response){
            dfd.resolve(response);
          });
          }, 1000);
        }
      
        return dfd.promise;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-10-05
        • 2022-11-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-24
        • 1970-01-01
        相关资源
        最近更新 更多