简体   繁体   English

禁用ng-click HTML DOM元素

[英]Disable ng-click on HTML DOM element

I have an <a> element which has ng-click attribute. 我有一个具有ng-click属性的<a>元素。 On certain conditions, I want to disable ng-click on this element. 在某些情况下,我想禁用ng-click此元素。 I'm using jquery to find this element and I'm trying to stop click by every possible way. 我正在使用jquery来找到这个元素,我试图通过各种可能的方式停止点击。 For example : return false ; 例如: return false ; event.stopPropagation() and etc. Also, I remove ng-click attribute with event.stopPropagation()等。另外,我删除了ng-click属性

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

,but event still firing. ,但事件仍在解雇。 Is it possible to stop (disable) ng-click by jquery ? 是否可以通过jquery停止(禁用) ng-click

First Option 第一选择

You can always add disabled attribute on the anchor element to prevent ng-click from triggering. 您始终可以在锚元素上添加disabled属性,以防止触发ng-click You also need to use CSS when adding disabled attribute. 添加禁用属性时还需要使用CSS。 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 . 您可以使用指令删除ng-click But that will not work with jQuery ie if you add this directive using jQuery, it won't work. 但这不适用于jQuery,即如果你使用jQuery添加这个指令,它将无法正常工作。

 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: 如果你想以Angular方式进行,你应该在元素上使用ng-disabled,如:

<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. 除非你知道自己在做什么,否则替换DOM中的元素是一个非常糟糕的主意。 If there are any bindings the element need to be compiled 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. 我只需要用新的元素替换旧元素,但在此之前,必须删除属性然后它才能正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM