prevent multiple form submissions using angular.js - disable form button

后端 未结 9 744
耶瑟儿~
耶瑟儿~ 2020-12-13 21:22

I want to prevent multiple form submissions using angular.js. The question is related to this question.

When the user clicks on a form submit button the value / labe

9条回答
  •  鱼传尺愫
    2020-12-13 22:06

    An addition to spenthil answer, a variant in coffee script + you can enable a button back if you need (e.g. when a form validation has failed and you want to try again)

    class ClickOnceDirective
    
        constructor: (@$timeout) ->
            link = (scope, element, attrs) =>
    
                originalText = element.html()
                replacementText = attrs.clickOnce
    
                element.bind('click', =>
                    @$timeout ->
    
                        if (replacementText)
                            element.html(replacementText)
    
                        element.attr('disabled', true)
    
                        # enable back
                        @$timeout ->
                             element.attr('disabled', false)
                            if (replacementText)
                                 element.html(originalText)
                        , 500
    
                    , 0)
    
             return {
             link
             restrict: 'A'
            }
    
    directivesModule.directive 'clickOnce', ['$timeout', ClickOnceDirective]
    

提交回复
热议问题