DEV Community

Maciej Manista
Maciej Manista

Posted on

Angular - quick tip of the day: External links - "target" attribute

Let’s say we have a link and we want to open it in either new tab or same tab, depending on the value of a variable.
Let’s assume openInNewTab variable is false:

  • [attr.target]="openInNewTab ? '_blank' : null" - works fine, produces no target attribute

  • [target]="openInNewTab ? '_blank' : null" - produces target="null" as an attribute, which still opens the link in a new tab, just the same as if you used _blank

TL;DR: use [attr.target] for best results :)

Top comments (0)