mrded mrded - 3 months ago 9
AngularJS Question

How can I catch outbound link click on an Angular 1.x website?

I'm trying to catch link clicks to external websites, to track them later.

Something like

<a href="#" ng-click="click()">foo</a>
but across all DOM tree.

Most of a content is coming from external REST server, thus I cannot use
ng-click
there.

Thanks.

Answer

what you can do is write a directve named a

angular.module('tracking', [])
.directive('a', function() {
  return {
    restrict: 'E'
    link: function(scope, element, attrs) {
      element.on('click', function() {
        if (attrs.target == outside-target)
        //your logic
      })
    }
  }
}`

important logical part would be

     if (attrs.target == outside-target)
        //your logic
      })
Comments