Preventing / dealing with double button clicks in angular

前端 未结 13 1184
我寻月下人不归
我寻月下人不归 2020-11-27 19:53

In angular we can set up a button to send ajax requests like this in view:

... ng-click=\"button-click\"

and in controller:



        
13条回答
  •  难免孤独
    2020-11-27 20:43

    To expand again on zsong's doe :

    First, with this solution, people can use double click to use your app. Old people sometime do that (as they were used to double click to open a program on windows), and other people do that by mistake too.

    Second, the user can click as quickly as they can, their browser will wait for the server response before re-enabling the button (it is a fix for Mark Rajcok's comment on zsong's post : "If the AJAX request takes longer than the browser's double-click time/window, this won't work. I.e., a user could pause and click again.").

    in your html

    
    

    in your controller

    $scope.buttonClicked = function() {
        Service.doService.then(function(){
            //this is the callback for success
            // you probably do not want to renable the button here : the user has already sent the form once, that's it - but just if you want to :
            $scope.submitButtonDisabled --;
            //display a thank you message to the user instead
            //...
        }).error(function(){
            //this is the callback for the error
            $scope.submitButtonDisabled --;
        })
    }
    

提交回复
热议问题