5

How can I prevent AngularJS to not click on an HTML button tag that has "disabled" attribute?

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<button type="button" disabled ng-click="something()">No Click Please!</button>

In this case, something() should NOT be called.

5
  • Disabled attribute would not let make click event to happen, what is happening in your case? Commented Sep 6, 2016 at 18:19
  • check this baudehlo.com/2014/02/24/… Commented Sep 6, 2016 at 18:19
  • It asked many times check this stackoverflow.com/questions/32005918/… Commented Sep 6, 2016 at 18:20
  • Are you sure this is really a problem? Seems like disabled does prevent ng-click. See plnkr.co/edit/z3IURIaoMvaXsxVuFxE3 Commented Sep 6, 2016 at 18:46
  • It seems to be the effect of css-only effect to disable. When real 'disabled' is used, it fixed. Thank you all. Commented Sep 13, 2016 at 5:05

4 Answers 4

9

You pass an expression that has to be evaluated in the ngClick directive. If it is not evaluated true then something() will not be called.

Heres an example:

(function() {

  angular.module('MyApp', [])
    .controller('MyController', MyController);

  MyController.$inject = ["$scope"];

  function MyController($scope) {

    $scope.disabled = true;

  }

})();
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div data-ng-app="MyApp">

  <div data-ng-controller="MyController">

    <button type="button" ng-disabled="disabled" ng-click="disabled || something()">No Click Please!</button>

    <button type="button" data-ng-click="disabled = !disabled">{{disabled ? 'Enable' : 'Disable'}}</button>

  </div>

</div>

Notice you can also use the ngDisabled directive so that if $scope.disabled is true, something() will not be called and the button will also be disabled!

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

1 Comment

Should be disabled || something() not &&
0

Proper way is to use ng-disabled which is a default directive in angular and using a scope variable to handle it to make it disabled,

HTML:

 <button ng-click="disableClick()" ng-disabled="isDisabled" ng-model="isDisabled">No Click Please!</button>

JS:

    $scope.isDisabled = false;
    $scope.disableClick = function() {
        alert("Clicked!");
        $scope.isDisabled = true;
        return false;
    }

EDIT

As papakia's mentioned, check the boolean variable and function so that it will not get executed.

DEMO APP

5 Comments

No, as the question states, something() should NOT be called. This fails.
@papakia Though it does not provide an answer, definitely it helps. if you have downvoted it, no comments
I did down vote this answer because the question was How can I prevent AngularJS to not click on an HTML button tag that has "disabled" attribute?. This answer gives the impression that the ngDisabled directive will do just that. On a side note, I have not down voted you before :)
It's not personal! I respect for your knowledge of the language :)
@papakia I did not take it personal dude! it happens , thanks for correcting me though!
0

This solution works in AngularJS for any and all click targets using the built-in disabled attribute for form inputs and/or other elements using Bootstrap's .disabled class and it works without adding to the controller.

HTML

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<button type="button" disabled="disabled" data-ng-click="something($event)">No Click Please!</button>

JavaScript

function something( $event )
{
    var is_enabled = !angular.element( $event.currentTarget ).is( '.disabled,:disabled' );
    if( is_enabled )
    {
    }
}

Comments

-3

Use ng-disabled.

<md-button ng-disabled="true"> Disabled Button </md-button>

EDIT ...

Fiddled

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.