我想在输入完成后触发一个函数
I would like to fire a function after the input has been complete
我想在输入完成后触发一个函数。去抖动功能对我不起作用。我不希望它聚焦,我希望在按键时触发的功能上点击更少。
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;
}
相关文章:
- 创建一个类似链接的按钮,并通过Javascript函数打开一个新的弹出窗口
- 我可以在json对象中添加一个函数吗
- Javascript使函数一个接一个地执行
- 是JavaScript中的函数一个对象
- 两个几乎相等的jQuery函数;一个适用于IE,一个不适用于IE
- 两个Javascript函数一个window.onload=Custom.init;和一个window.onload=f
- 使javascript加载函数一个接一个地执行
- 如何通过两个嵌套的匿名函数(一个带有超时的事件处理程序)传递变量
- 为什么我不能让两个jQuery函数一个在另一个里面呢?
- 给函数一个变量名,而不是它的值
- 如何使用嵌入函数(一个滚动页由Pete - peachananr)
- 是否有可能给JavaScript函数一个类型/类?
- 确保两个函数一个接一个地执行,其中第一个函数内部有一个异步调用
- 两个不同的ajax函数一个接一个调用返回相同的值
- jQuery第一次更改函数一个工作
- 给这个函数一个id
- 如何给回调函数一个值一个变量在特定时刻
- 使用回调使两个函数一个接一个地运行
- 两个函数(一个用php)是否可以用“;onclick”;
- 两个几乎相同的函数.一个函数用Queue进行排队.Jquery效果在queued函数上不起作用.为什么