Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent ng-click="" effect on a disabled button

Tags:

angularjs

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.

like image 892
Bimal Poudel Avatar asked Sep 06 '16 18:09

Bimal Poudel


People also ask

How do I disable a clicked button?

To disable a submit button, you just need to add a disabled attribute to the submit button. $("#btnSubmit"). attr("disabled", true); To enable a disabled button, set the disabled attribute to false, or remove the disabled attribute.

What is ng-disabled?

Definition and Usage. The ng-disabled directive sets the disabled attribute of a form field (input, select, or textarea). The form field will be disabled if the expression inside the ng-disabled attribute returns true. The ng-disabled directive is necessary to be able to shift the value between true and false .

How do you disable a button until another button is clicked in Angular?

You can disable a button in angular after it clicked by bounding the button disabled property to a Boolean variable in the component.

How to disable input in AngularJS?

The ng-disabled Directive in AngularJS is used to enable or disable the HTML elements. If the expression inside the ng-disabled attribute returns true then the form field will be disabled or vice versa. It is usually applied on the form field (i.e, input, select, button, etc).


1 Answers

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!

like image 153
cnorthfield Avatar answered Oct 21 '22 07:10

cnorthfield