如何在AngularJS中进行ng-click事件 [英] How to make ng-click event conditional in AngularJS
问题描述
我在ng-repeat里面有这个代码:
< a href =#class =disabledng -click =doSomething(object)>做点什么< / a>
如何在 class =disabled
。
如何使用该类在HTML中创建条件,
或者有没有办法在javascript中看起来像这样:
($。(this).hasClass('disabled')){
做某事
}
});
除了指令之外的任何地方。你可以添加一些值来指示链接是否被禁用。
但是其他问题是ngDisabled不能用于除窗体控件之外的任何其他功能,所以你不能使用它与< a>,但是您可以将其与< button>一起使用并将其设置为链接。 另一种方式是使用像 isDisabled || action()
因此,如果 isDisabled
为true,则不会调用action。
两种解决方案: http://plnkr.co/edit/5d5R5KfD4PCE8vS3OSSx?p=preview
I have this code inside ng-repeat:
<a href="#" class="disabled" ng-click="doSomething(object)">Do something</a>
How to make a condition that the button is disabled when it has class="disabled"
.
How can I make the condition in the HTML using the class,
or is there a way to do it in the javascript that will look like:
$('.do-something-button').click(function(){
if (!$(this).hasClass('disabled')) {
do something
}
});
It is not good to manipulate with DOM (including checking of attributes) in any place except directives. You can add into scope some value indicating if link should be disabled.
But other problem is that ngDisabled does not work on anything except form controls, so you can't use it with <a>, but you can use it with <button> and style it as link.
Another way is to use lazy evaluation of expressions like isDisabled || action()
so action wouold not be called if isDisabled
is true.
Here goes both solutions: http://plnkr.co/edit/5d5R5KfD4PCE8vS3OSSx?p=preview
这篇关于如何在AngularJS中进行ng-click事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!