最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Disable ng-click on HTML DOM element - Stack Overflow

programmeradmin2浏览0评论

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 ?

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 ?

Share Improve this question edited Jun 16, 2016 at 10:53 Shashank Agrawal 25.8k11 gold badges96 silver badges125 bronze badges asked Jun 16, 2016 at 10:25 Harry BirimirskiHarry Birimirski 1,0581 gold badge11 silver badges23 bronze badges 3
  • Possible duplicate of unbind ng-click dynamically from element : angularjs – rrk Commented Jun 16, 2016 at 10:28
  • What about adding a condition in your ng-click function back in the controller? – AranS Commented Jun 16, 2016 at 10:30
  • Why not add ng-disabled which evaluates to your condition – ebram khalil Commented Jun 16, 2016 at 10:31
Add a ment  | 

3 Answers 3

Reset to default 3

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./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
    pile: function(element, attr) {
      attr.ngClick = null;
    }
  }
});
<script src="https://ajax.googleapis./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 piled first.

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

I was so close.. find a solution ..

var temp = $(result.element).removeAttr('ng-click');
$(result.element).replaceWith(temp);

I just need to replace old element with new one but before that , must remove attribute and then it's working.

发布评论

评论列表(0)

  1. 暂无评论