方法一:點(diǎn)擊后,讓button的狀態(tài)變?yōu)閐isable
js指令:
.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); }) }); } }; })html:
代碼如下:
<button type="button" class="btn btn-info btn-bordered waves-effect w-md waves-light" click-and-disable="next()">下一步</button> //把 ng-click 改為指令click-and-disable
方法二:在app.config里面,重寫ng-click事件,設(shè)置一定事件內(nèi)不能重復(fù)點(diǎn)擊
$provide.decorator('ngClickDirective',['$delegate','$timeout', function ($delegate,$timeout) { //記得在config里注入$provide var original = $delegate[0].compile; var delay = 500;//設(shè)置間隔時(shí)間 $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; }]);以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持錯(cuò)新站長站。
新聞熱點(diǎn)
疑難解答
圖片精選