Home > Web Front-end > JS Tutorial > angular ng-click prevents repeated submission of instances

angular ng-click prevents repeated submission of instances

零下一度
Release: 2017-06-24 14:32:13
Original
1722 people have browsed it

Method 1:After clicking, change the status of the button to disable

js command:

.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);
                    })
                });
            }
        };
    })
Copy after login

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
Copy after login
<br>
Copy after login

Method 2: In app.config, rewrite the ng-click event and set it to prevent repeated clicks within a certain event

$provide.decorator('ngClickDirective',['$delegate','$timeout', function ($delegate,$timeout) {  //记得在config里注入$provide
            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;
        }]);
Copy after login

The above is the detailed content of angular ng-click prevents repeated submission of instances. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template