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 ?
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).
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 .
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.
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.
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>
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>
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With