Having a text input I want to know immediately when the user is changing the text, but also I want to use the debounce feature. This way I can, for example, disable a submit button while the user is changing the text and enable the submit button after checking the text in the debounced function.
Is there a way to do this with pure AngularJS? Or should I use javascript/jquery?
With this code I am only able to know when the user has changed the text after the debounce 500ms delay:
<!doctype html>
<html ng-app="app">
<head>
<script src="http://localhost/js/angular.min.js"></script>
<script>
var app= angular.module('app',[]);
app.controller('ExampleController',['$scope',function($scope){
$scope.changed= '';
$scope.change= function(){
$scope.changed= 'Changed!';
};
}]);
</script>
</head>
<body ng-controller="ExampleController">
<div>Message: <input type="text" ng-model="model.message"
ng-model-options="{debounce:500}" ng-change="change()" />
<div>{{model.message}}</div>
<div>{{changed}}</div>
</body>
</html>
Your main option is to write your own debouncing code using
ng-keyup
. Every time a key is pressed, you will be notified of the change (and the change will be present in theng-model
value) and you can use your ownsetTimeout
there, with the desired debounce function as the callback. If the timeout is already set, simply cancel it and restart it on each key press.