DEV Community


How to verify link clicked in Angular html template

ama profile image Adrian Matei Originally published at ・3 min read

Being on a constant lookout for optimisation when managing my bookmarks on, I had this idea lately to add a bookmark to my history not only when I click the title of the bookmark (main URL), but also when I click hyperlinks in the description of the bookmark - sometimes I tend to bookmark the "parent" url and add "child" or related bookmarks in the description (an example would be bookmarking the same "resource" which is deployed on different environments like dev, test and prod):

Demo showing this in action

This plays really well now with the recently introduced hot keys to access the history and pinned bookmarks

Well, let's see how this looks like in code:

The HTML template

      (click)="onClickInDescription($event, bookmark)"
      (auxclick)="onMiddleClickInDescription($event, bookmark)"
      (contextmenu)="onRightClickInDescription($event, bookmark)">

In the template we listen to the following events the Angular API provides:

  • onClick - fires when the user clicks the left mouse button on the object
  • onauxclick - to cover the middle button click of the mouse I use the auxclick1 event, which is fired at an Element when a non-primary pointing device button (any mouse button other than the primary—usually leftmost—button) has been pressed and released both within the same element.fires when the user clicks the left mouse button on the object
  • oncontextmenu - fires when the user clicks the right mouse button in the client area, opening the context menu.

The Angular component

Let's see how this is implemented in the component:

  onClickInDescription($event: any, bookmark: Bookmark) {
    if (this.isHtmlAnchorElement($event)) {
      $'target', '_blank');

  onRightClickInDescription($event: any, bookmark: Bookmark) {
    if (this.isHtmlAnchorElement($event)) {

  onMiddleClickInDescription($event: any, bookmark: Bookmark) {
    if (this.isHtmlAnchorElement($event)) {

  private isHtmlAnchorElement($event: any) {
    return $'a');

The pattern is the same: verify that the object onto which the event was dispatched is an HTML Anchor - isHtmlAnchorElement(). If that's the case it will be added to my bookmarks history.

To achieve that, we use the target2 property of the Event interface with the matches(selector)3 method. In this particular case we use a as selectorString. For other html elements use a corresponding selector.


  • on left click we force opening the URL in a new tab by setting the target attribute to _blank - $'target', '_blank');
  • on right click I haven't found a way yet to verify if the user clicked "Open Link in New Tab" or "Open Link in New Window" - suggestions are more than welcomed; but for the time being I think it is enough, what else would you do a right click on a link for?


This is definitely a piece of code I will bookmark for later.





Discussion (0)

Editor guide