简体   繁体   English

Angular JS - 将onclick事件附加到页面上的所有链接

[英]Angular JS - Attach onclick event to all links on a page

I have an AngularJS application. 我有一个AngularJS应用程序。 A requirement has come up where I have to track whenever a user clicks on an external link ( <a href="http://some-other-site.com/offers">click here for offers</a> ). 我需要在用户点击外部链接时进行跟踪( <a href="http://some-other-site.com/offers">click here for offers</a> )。

Normally in jQuery I would write something like the following: 通常在jQuery中我会写如下内容:

$(document).on('click', 'a[href^="http://"]', function() {
    // perform tracking here
    return true;
});

But, I'm trying to avoid using jQuery and do things the Angular way. 但是,我试图避免使用jQuery并以Angular的方式做事。 But what is the best way to handle this in pure Angular? 但是在纯Angular中处理这个问题的最佳方法是什么? I don't want to resort to putting ng-click's on all the external links, as these will constantly change as new functionality is added. 我不想在所有外部链接上使用ng-click,因为随着新功能的添加,这些链接会不断变化。

You can set a directive directly on the a tag to ensure all a tags within your application get the directive attached to them. 您可以直接在标签设置指令,以确保您的应用程序内的所有标签一个得到重视他们的指令。

.directive('a', function () {
    return {
         restrict: 'E',
         link: function (scope, elem, attrs) {
              // your code here
         }
    }
})

创建一个匹配<a>元素的元素指令,并在那里添加一个单击处理程序。

I'm late to the party but I would like to put your question in another perspective: 我迟到了,但我想从另一个角度提出你的问题:

What is the "angular way" in this context? 在这种情况下,“角度方式”是什么?

I was asking myself the same questions and I came up witch following conclusion: 我问自己同样的问题,我得出结论:

  • " Angular way " is not the opposite of " jQuery way " simply because jQuery is not a framework (jQuery is something every developer wonders why it's not the default in every browser) Angular方式 ”并不是“ jQuery方式 ”的反面,因为jQuery不是一个框架(jQuery是每个开发人员都想知道为什么它不是每个浏览器中的默认值)
  • I should go the " Angular way " always when the task is to change the model data in the application 当任务是更改应用程序中的模型数据时,我应该始终使用“ Angular方式

In your case tracking should be added to external links so I assume no application related data is affected by this change. 在您的情况下,应将跟踪添加到外部链接,因此我假设此更改不会影响与应用程序相关的数据。 In this case I would go with a solution which is the easiest to maintain (because we all know that later someone will want either to remove the tracking or add another tracking or both). 在这种情况下,我会选择最容易维护解决方案 (因为我们都知道以后有人会想要删除跟踪或添加其他跟踪或两者兼而有之)。

1) As already mentioned in the comments, there is pure js way to do it: 1)正如评论中已经提到的,有纯粹的js方法可以做到:

var externalLinks = document.querySelectorAll('a[href^="http://"]');
for (i = 0; i < externalLinks.length; ++i) {
    var link = externalLinks[i];
    link.addEventListener('click', function(e) {
        // track me
    });
}

http://jsfiddle.net/a7fg217h/1/ http://jsfiddle.net/a7fg217h/1/

2) And one another angular solution: 2)另一个角度解决方案:

.directive('extLink', function() {
  return {
    restrict: 'A',
    link: function(scope, elem) {
      console.log(elem);
      elem.bind('click', function(e) {
        console.log(e);
      });
    }
  };
})

The only difference to the already posted answer is that you use this directive as an attribute: 与已发布的答案的唯一区别是您将此指令用作属性:

<a ext-link href="http://google.com">Link</a>

http://plnkr.co/edit/OhPfEzwDK3j4d17O86oo?p=preview http://plnkr.co/edit/OhPfEzwDK3j4d17O86oo?p=preview

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

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