In Angular, utilizing both the <code>ng-click
and ng-href
directives at the same time will result in the click function being executed first. In this scenario, clicking on a link that navigates to Google will be prevented and instead an alert will be displayed.
<a ng-href='http://google.com' ng-click="click($event)">Link</a>
$scope.click = function (event) {
event.preventDefault();
alert('click has been triggered');
}
It's worth noting that this will only work if the user uses the left mouse button to click the link. If the user attempts to open the link in a new tab, the click event won't be triggered. While this behavior may seem logical, as it isn't technically a "click", is there an Angular directive available for handling the opening of links in new tabs?
UPDATE:
I am not looking to programmatically open a new tab, but rather to handle the event of a user opening a new tab themselves.