0

A tag looks like this:

<a class="someClass"[routerLink]="[PATH, item.id]" [queryParams]="{category: item.parent.category, childItem:item.parent.childItem}">{{item.parent.name}}</a>

I tried adding a click event with a function to it, and I tried adding target="_blank" but for some reason, it doesn't behave like a link. I guess it is because of internal routing but not quite sure. I am new to Angular, however, I found only answers to how to open a new tab straight away or in the new window not how to allow it to open in a new tab or window on right click with the browser's built-in context window.

Xab Ion
  • 1,105
  • 1
  • 11
  • 20
Dekla
  • 1
  • 1

1 Answers1

0

The reason you may not see the "Open in new tab" option in the context menu when right-clicking on the anchor tag is because of the way Angular handles navigation using [routerLink].

When you click on a link with the [routerLink] attribute, Angular intercepts the click event and uses its router to navigate to the specified route without actually navigating to a new page. This means that the context menu options related to opening links in a new tab or window are not applicable in this case.

To open the link in a new tab or window, you can still use the browser's built-in options. You can do this by either:

  • Hold down the Ctrl (Windows) or Cmd (Mac) key while clicking on the link. This will open the link in a new tab.

  • Right-click on the link and select "Copy link address" or "Copy link location" from the context menu, then open a new tab or window and paste the link into the address bar.

  • Note that if you want to explicitly allow opening the link in a new tab or window, you can add the target="_blank" attribute to the anchor tag, like this:

<a class="someClass"
   [routerLink]="[PATH, item.id]"
   [queryParams]="{category: item.parent.category, childItem:item.parent.childItem}"
   target="_blank"
>
   {{item.parent.name}}
</a>

However, using target="_blank" can be a security risk if the link is controlled by an attacker. This can lead to what is known as a "tab-nabbing" attack, where the attacker can replace the content of the new tab with malicious content. Therefore, it is generally recommended to avoid using target="_blank" unless you trust the content of the link.

cursorrux
  • 1,382
  • 4
  • 9
  • 20
Ope_D1
  • 1
  • Is there a way to get href and left corner of the browser address visible without losing routerLink functionality of browser not refreshing? – Dekla Feb 27 '23 at 06:50