如何使ng-click事件有条件? [英] How to make an ng-click event conditional?

查看:189
本文介绍了如何使ng-click事件有条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ng-repeat中有这个代码:

I have this code inside ng-repeat:

<a href="#" class="disabled" ng-click="doSomething(object)">Do something</a>

如何在按钮被 class =disabled?

How to make a condition that the button is disabled when it has class="disabled"?

或者有没有办法在Javascript中执行此操作,如下所示: / p>

Or is there a way to do it in Javascript so that will look like:

$('.do-something-button').click(function(){
  if (!$(this).hasClass('disabled')) {
    do something
  }
});


推荐答案

用DOM操作是不好的(包括检查属性)除了指令之外的任何地方。您可以在范围内添加一些值,指示是否应禁用链接。

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.

但是其他问题是ngDisabled不能处理除表单控件之外的任何内容,因此您无法使用它与< a>一起使用,但您可以将它与< button>一起使用并将其设置为链接。

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.

另一种方法是使用像<$这样的表达式的惰性求值c $ c> isDisabled || action()如果 isDisabled 为真,则不会调用操作。

Another way is to use lazy evaluation of expressions like isDisabled || action() so action wouold not be called if isDisabled is true.

这里两种解决方案: http://plnkr.co/edit/5d5R5KfD4PCE8vS3OSSx?p=preview

这篇关于如何使ng-click事件有条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆