1

I have this directive to intercept link click:

var app = angular.module('mobApp.services');
app.directive('a', function() {
    return {
        restrict: 'E', // only Elements (<a>),
        link: function(scope, elm, attr) {
            elm.on('click', function($event) {
                $event.preventDefault()
                var href = attr.href;
                if(!!href && href !== '#') {
                    window.open(href, '_system', 'location=yes');
                }
                return false
            })
        }
    }
})

I have this element

<p ng-bind-html="post.details | linky:'_blank'" class="ng-binding">
   <a target="_blank" href="https://davidwalsh.name/speech-recognition" class="">
        https://davidwalsh.name/speech-recognition
   </a>
</p>

clicking on a is not firing the above directive code.

georgeawg
  • 48,608
  • 13
  • 72
  • 95
Manish Kumar
  • 10,214
  • 25
  • 77
  • 147

1 Answers1

1

It doesn't work because the html markup that you render via ng-bind-html has not been compiled by Angular in order to directives could work. So if you want to add directive to the document dynamically, you have to compile it. Here is an answer of how you can do that.

Community
  • 1
  • 1
GProst
  • 9,229
  • 3
  • 25
  • 47