废话不多说,直接上代码。
1:html页面
<!DOCTYPE html>
<html ng-app="plunker">
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script>document.write('<base href="' + document.location + '" />');</script>
<link rel="stylesheet" href="style.css" />
<script data-require="angular.js@1.2.x" src="http://code.angularjs.org/1.2.15/angular.js" data-semver="1.2.15"></script>
<script src="app.js"></script>
</head>
<body ng-controller="MainCtrl">
<button click-and-disable="functionThatReturnsPromise()">Click me</button>
</body>
</html>
2:js文件(控制器)
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope, $timeout) {
$scope.functionThatReturnsPromise = function() {
return $timeout(angular.noop, 1000);//这里可以设置时间,改文字可以删除
}
});
app.directive('clickAndDisable', function() {
return {
scope: {
clickAndDisable: '&'
},
link: function(scope, iElement, iAttrs) {
iElement.bind('click', function() {
iElement.prop('disabled',true);
scope.clickAndDisable().finally(function() {
iElement.prop('disabled',false);
})
});
}
};
});