If you're working with Angular, you have the ability to create a custom directive for the <a>
element and attach a click listener to it.
app.directive('a', function() {
return {
restrict: 'E', // only Elements (<a>),
link: function(scope, elm, attr) {
// Triggered each time the link is clicked
elm.on('click', function($event) {
console.log('event')
$event.preventDefault()
return false
})
}
}
})
And just like magic!
If you need to restrict access to certain URLs, you can retrieve the href attribute within the link function using attr.href
. Here's an example of how you can achieve this:
app.directive('a', function() {
return {
restrict: 'E', // only Elements (<a>),
link: function(scope, elm, attr) {
// Triggered each time the link is clicked
elm.on('click', function($event) {
// Restrict specific URLs
if(attr.href.match(/youtube\.com/)) {
$event.preventDefault()
return false
}
})
}
}
})