Preventing / dealing with double button clicks in angular

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

    Using ng-disabled worked just fine in this example. No matter how furiously I clicked the console message only populated once.

    var app = angular.module('plunker', []);
    
    app.controller('MainCtrl', function($scope) {
      $scope.submitData = function() {
        $scope.buttonDisabled = true;
        console.log("button clicked");
      }
    
      function augment() {
        var name, fn;
        for (name in $scope) {
          fn = $scope[name];
          if (typeof fn === 'function') {
            if (name.indexOf("$") !== -1) {
              $scope[name] = (function(name, fn) {
                var args = arguments;
                return function() {
                  console.log("calling " + name);
                  console.time(name);
                  fn.apply(this, arguments);
                  console.timeEnd(name);
                }
              })(name, fn);
            }
          }
        }
      }
    
      augment();
    });
    
    
    
    
      
      AngularJS Plunker
      
      
      
    
    
    
      
    
    
    

    I was curious exactly how long it takes for angular to apply the changes to the buttonDisabled flag. If you check the console in the plunker example it displays how long it takes the $eval and $apply methods to execute. On my machine it took an average of between 1-2 milliseconds.

提交回复
热议问题