在AngularJS如何使一个条件NG点击 [英] How to make a condition on ng-click in AngularJS

查看:211
本文介绍了在AngularJS如何使一个条件NG点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有NG重复这里面code:

 < A HREF =#级=已禁用NG点击=DoSomething的(对象)>做一些< / A>

如何使一个条件,即该按钮被禁用时,它具有类=已禁用

我如何使用类使病情在HTML中,

还是有办法做到这一点在JavaScript中,这将是这样的:

  $('。做出头的按钮')。点击(函数(){
  如果(!$(本).hasClass(禁用)){
    做一点事
  }
});


解决方案

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

但另一个问题是,ngDisabled不上,除了窗体控件的任何工作,所以你不能以&lt使用它;一>,但你可以以&lt使用它;按钮>和风格作为链接

另一种方法是使用前pressions懒评价像 isDisabled ||行动()这样的行动wouold不能称之为如果 isDisabled 是真实的。

下面是双方的解决方案:<一href=\"http://plnkr.co/edit/5d5R5KfD4PCE8vS3OSSx?p=$p$pview\">http://plnkr.co/edit/5d5R5KfD4PCE8vS3OSSx?p=$p$pview

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点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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