在angularjs preventing多次点击 [英] Preventing multiple clicks in angularjs
本文介绍了在angularjs preventing多次点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我想从我的页面上删除一条记录,我需要在删除前,显示确认消息。为此,我已经用锚和 NG-点击='删除()
删除这样的行。在这里,用户可以点击锚多次。这是一个严重的问题,因为确认弹出渲染多次。我的例子是一个示例。在项目中,我遇到这样的问题太多。我想prevent多点击延长 NG-点击
。
When I want to delete a record from my page I need to show confirmation message before deleting. For this I have used anchor and ng-click='delete()'
for deleting such row. Here user can click on anchor multiple times. It is a serious problem because confirmation popup render multiple times. My example is a sample. In my project I have faced too many problems like that. I want to prevent multi click to extend ng-click
.
推荐答案
如果我们覆盖ngClick指令,那么我们就可以保护多的点击次数。
If we override ngClick directive then we can protect multiple clicks.
app.config(['$provide', function ($provide) {
$provide.decorator('ngClickDirective',['$delegate','$timeout', function ($delegate,$timeout) {
var original = $delegate[0].compile;
var delay = 500;
$delegate[0].compile = function (element, attrs, transclude) {
var disabled = false;
function onClick(evt) {
if (disabled) {
evt.preventDefault();
evt.stopImmediatePropagation();
} else {
disabled = true;
$timeout(function () { disabled = false; }, delay, false);
}
}
// scope.$on('$destroy', function () { iElement.off('click', onClick); });
element.on('click', onClick);
return original(element, attrs, transclude);
};
return $delegate;
}]);
}]);
这篇关于在angularjs preventing多次点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文