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

查看:22
本文介绍了如何使 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 中做到这一点,看起来像:

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 对表单控件以外的任何东西都不起作用,因此您不能将它与 一起使用,但您可以将它与

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