0

I have a problem with a button which contacts server on click. If you do a double click (or any number of clicks for that matter) you will call the server that number of times.

<div class="modal-footer">
    <button type="button" class="btn btn-default" data-dismiss="modal">{{ 'ADMIN.CONTENT.DELIVERIES.BODY.CLOSE' | translate }}</button>
    <a class="btn btn-danger" ng-click="vm.markDelivered()" ng-dblclick="return" ng-disabled="flag">{{ 'MANAGER.CONTENT.DELIVERIES.BODY.DELETE_PANEL.CONFIRM' | translate }}</a>
</div>

I have tried with ng-disabled but for some reason it doesn't work, as it is saying that that element is not allowed there. I tried changing a to button, but that seems the same. ng-dblclick="return" does nothing also.

4 Answers 4

2

Even I had the same issue,And solved using this approach.

<div class="col-sm-4 form-group pull-right">
   <input type="submit" name="Submit" class="btn btn-primary"
    value="Submit" data-ng-disabled="myForm.$invalid"
    ng-click="myForm.$invalid=true;vm.markDelivered()" />
</div>

So on first click myForm.$invalid=true will be set and button will be disabled. SO you will not have multiple calls to your server side code.

Sign up to request clarification or add additional context in comments.

2 Comments

Amazing, slick, simple and effective
Yeah that even disables your submit button if form validations are not proper.Glad it helped you..!!
1

So with Bootstrap buttons you won't be able to use ng-disabled. You would have to do it this way:

<div class="btn btn-default" ng-class="{'disabled': idDisabled}" ng-click="doSomething()">I'm a button!</div>

where you are setting the class disabled on the button. But this does not disable the action itself. So when the button is pressed you would need to check that isDisabled variable and if it is true just return and don't do the intended action.

So for example:

doSomething() {
   if (isDisabled) {
       return
   } else {
       // do your server call
       // when call is finished set isDisabled = false
   }
}

8 Comments

Have to use what? :)
He meant to say use <button> instead of <a>
I tried that one also, but it is still telling me that it is not allowed there?
Oh I know what the issue is I think, I'll edit my answer above
Just updated my answer. It's because you are using Bootstrap and for some reason bootstrap doesn't work with ng-disabled
|
0

I see a couple of issues here.

First, change the anchor tag to a button.

Second, you seem to be using 'controller as' syntax. So, you are probably setting your flag in the vm. But, in your html, you are looking for the flag in the $scope. Change ng-disabled="flag" to ng-disabled="vm.flag"

Your final button line would look like this: <button class="btn btn-danger" ng-click="vm.markDelivered()" ng-dblclick="return" ng-disabled="vm.flag">{{ 'MANAGER.CONTENT.DELIVERIES.BODY.DELETE_PANEL.CONFIRM' | translate }}</button>

Adding a working plunker here demonstrating ng-disabled

2 Comments

it is telling me that Attribute ng-disabled is not allowed here? Do you know why is that?
I've added a working plunker demonstrating ng-disabled for a button. Note, it won't work for an <a>. You have to change it to a <button>
0

What about using a simple behaviour directive?

function oneTimeCallDirective($timeout) {
  var httpCallMock = (cb = () => 'ok') => $timeout(cb, 5000);

  return function oneTimeCallPostLink(iScope, iElement) {
    let busy = false;
    
    return iElement
      .on('click dblclick', function(event) {
        if(busy) {
          return event.preventDefault();
        }
        
        busy = true;
        
        console.info('Well, Calling.');        
        
        return httpCallMock()
          .then(() => {
            console.log('You Can Start Calling Again');
          })
          .finally(() => {
            busy = false;
          })
        ;
      })
    ;
  };
}

angular
  .module('test', [])
  .directive('oneTimeCall', oneTimeCallDirective)
;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<section ng-app="test">
  <button one-time-call>Click Here</button>
</section>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.