简体   繁体   English

如何避免触发在AngularJS中单击链接时触发的onclick()事件

[英]How to avoid firing an onclick() event fired when clicking a link in AngularJS

First of all, I want to say that I have seen some topics already with similar question. 首先,我想说我已经看到了一些已经有类似问题的话题。 I have tried withe the responses of these topics, but I have not been able to find a solution for my problem. 我已经尝试了这些主题的响应,但是无法为我的问题找到解决方案。 Maybe the difference is that I am using AngularJS 也许不同之处在于我正在使用AngularJS

(HTML) (HTML)

<li class="article-list_item" ng-repeat="noticia in news" >
    <article class="article_news" id="newsBlock{{noticia.id}}" >    
        <header>
          <h1 class="article_newsTitle"> 
              <a href="http://www.google.es" rel="tooltip" title="{{noticia.title}}"
                   data-toggle="modal" ng-bind-html-unsafe="noticia.shortTitle"></a>
          </h1>
        </header>   
    </article>

(JS) (JS)

$(document).ready(function () {
     $(".article_news").click(function (event) {
         $(this).addClass("approved"); 
     });
});

What I am trying to do is to add the class "Approved" to the object I am clicking. 我想做的是将类“ Approved”添加到我单击的对象中。

But when I click the link inside the <article> , I dont want to add the Class. 但是,当我单击<article>内的链接时,我不想添加Class。 Instead, I want the browser to open the URL. 相反,我希望浏览器打开URL。

How can I do this? 我怎样才能做到这一点? Must I use stopPropagation() , preventDefault() or similar? 我必须使用stopPropagation()preventDefault()还是类似的方法? How should I use them? 我应该如何使用它们?

Thanks in advance 提前致谢

Well, Angular automatic filter link action if you implement it in angular way. 好吧,如果您以有角度的方式实施,则有角自动过滤器链接动作。 For implementing it in angular way you should create directive to bind click event which handles your issue. 为了以角度的方式实现它,您应该创建指令以绑定处理您的问题的click事件。 Still you can solve it like this. 仍然可以像这样解决它。

ng-click="$event.stopPropagation()"

Your HTML 您的HTML

     <article class="article_news" id="newsBlock{{noticia.id}}" >    
        <header>
          <h1 class="article_newsTitle"> 
              <a ng-click="$event.stopPropagation()" href="http://www.google.es" rel="tooltip" title="{{noticia.title}}"
                   data-toggle="modal" ng-bind-html-unsafe="noticia.shortTitle"></a>
          </h1>
        </header>   
    </article>

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

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