我想在输入完成后触发一个函数

I would like to fire a function after the input has been complete

本文关键字:函数 一个 输入      更新时间:2023-09-26

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

plnkr.co/edit/uDgRNnVh0NUm4z2BbzSj?p=preview 

在这个答案的帮助下,我让它按照(我希望)你所期望的那样工作。

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

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

<input type="text" ng-blur="foo()">
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;
}