Preventing / dealing with double button clicks in angular
First you'd better add ngDblclick, when it detects the double click just return false
:
<ANY ng-click="buttonClicked()" ng-dblclick="return false">
If you want to wait for the Ajax call to be finished, then you can disable the button by setting the ng-disabled
<ANY ng-click="buttonClicked()" ng-dblclick="return false;" ng-disabled="flag">
And in your controller, you can do
$scope.flag = false;
$scope.buttonClicked = function() {
$scope.flag = true;
Service.doService.then(function(){
//this is the callback for success
$scope.flag = false;
}).error(function(){
//this is the callback for the error
$scope.flag = false;
})
}
You need to handle both case when the ajax call is successfull or failed, since if it is failed, you don't want it show as diabled to confuse user.
Using ng-disabled
worked just fine in this example. No matter how furiously I clicked the console message only populated once.
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
$scope.submitData = function() {
$scope.buttonDisabled = true;
console.log("button clicked");
}
function augment() {
var name, fn;
for (name in $scope) {
fn = $scope[name];
if (typeof fn === 'function') {
if (name.indexOf("$") !== -1) {
$scope[name] = (function(name, fn) {
var args = arguments;
return function() {
console.log("calling " + name);
console.time(name);
fn.apply(this, arguments);
console.timeEnd(name);
}
})(name, fn);
}
}
}
}
augment();
});
<!doctype html>
<html ng-app="plunker">
<head>
<meta charset="utf-8">
<title>AngularJS Plunker</title>
<link rel="stylesheet" href="style.css">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.js"></script>
<script src="app.js"></script>
</head>
<body ng-controller="MainCtrl">
<input type="button" ng-click="submitData()" ng-disabled="buttonDisabled" value="Submit" />
</body>
</html>
I was curious exactly how long it takes for angular to apply the changes to the buttonDisabled
flag. If you check the console in the plunker example it displays how long it takes the $eval
and $apply
methods to execute. On my machine it took an average of between 1-2 milliseconds.