Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable ng-click on HTML DOM element

I have an <a> element which has ng-click attribute. On certain conditions, I want to disable ng-click on this element. I'm using jquery to find this element and I'm trying to stop click by every possible way. For example : return false ; event.stopPropagation() and etc. Also, I remove ng-click attribute with

$(element).removeAttr('ng-click');

,but event still firing. Is it possible to stop (disable) ng-click by jquery ?

like image 266
Harry Birimirski Avatar asked Jun 16 '16 10:06

Harry Birimirski


People also ask

What is ng-disabled?

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).

How to disable input field in angular HTML?

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 .

What is the difference between Ng-click and Onclick?

Another significant difference between ng-click and onclick is the execution context. Code inside an onclick attribute executes against the global window object, while an expression inside of ng-click executes against a specific scope object, typically the scope object representing the model for the current controller.

What is the use of NG-click?

The ng-click Directive in AngluarJS is used to apply custom behavior when an element is clicked. It can be used to show/hide some element or it can pop up an alert when the button is clicked.


2 Answers

First Option

You can always add disabled attribute on the anchor element to prevent ng-click from triggering. You also need to use CSS when adding disabled attribute. See the working example below

var app = angular.module("sa", []);

app.controller("FooController", function($scope) {

  $scope.sayHello = function() {
    alert("Hello!!");
  };
});
a[disabled] {
  pointer-events: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="sa" ng-controller="FooController">

  <a href="" disabled ng-click="sayHello()">Say Hello</a>
</div>

Second option

You can make use of a directive to remove ng-click. But that will not work with jQuery i.e. if you add this directive using jQuery, it won't work.

var app = angular.module("sa", []);

app.controller("FooController", function($scope) {

  $scope.sayHello = function() {
    alert("Hello!!");
  };
});

app.directive("disableclick", function() {
  return {
    restrict: 'A',
    priority: 1000,    // setting higher priority to let this directive execute before ngClick
    compile: function(element, attr) {
      attr.ngClick = null;
    }
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="sa" ng-controller="FooController">

  <a href="" disableclick ng-click="sayHello()">Say Hello</a>
</div>
like image 97
Shashank Agrawal Avatar answered Sep 23 '22 13:09

Shashank Agrawal


If you want to do it the Angular way you should use ng-disabled on the element like:

<a ng-click="someClickHandler()" ng-disabled="isDisabled">Link</a>

Replacing elements in the DOM is a very bad idea unless you know what you are doing. If there are any bindings the element need to be compiled first.

If this doesn't work try to replace a element with button.

like image 38
masitko Avatar answered Sep 22 '22 13:09

masitko