Preventing / dealing with double button clicks in angular

前端 未结 13 1079
我寻月下人不归
我寻月下人不归 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:47

    First you'd better add ngDblclick, when it detects the double click just return false:

    <ANY ng-click="buttonClicked()" ng-dblclick="return false">
    

    If you want to wait for the Ajax call to be finished, then you can disable the button by setting the ng-disabled

    <ANY ng-click="buttonClicked()" ng-dblclick="return false;" ng-disabled="flag">
    

    And in your controller, you can do

    $scope.flag = false;
    $scope.buttonClicked = function() {
        $scope.flag = true;
        Service.doService.then(function(){
            //this is the callback for success
            $scope.flag = false;
        }).error(function(){
            //this is the callback for the error
            $scope.flag = false;
        })
    }
    

    You need to handle both case when the ajax call is successfull or failed, since if it is failed, you don't want it show as diabled to confuse user.

    0 讨论(0)
提交回复
热议问题