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

时间:2016-03-10 10:13:02

标签: javascript angularjs underscore.js

我想在输入完成后启动一个功能。去抖功能对我不起作用。我不希望它聚焦在一起我希望功能上的命中次数减少在按键时触发。

plnkr.co/edit/uDgRNnVh0NUm4z2BbzSj?p=preview 

3 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

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 :(得分:0)

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