Home > Mobile >  Event for Open a link in new tab typescript angular
Event for Open a link in new tab typescript angular

Time:01-16

I am using Angular 8 and there is an anchor tag where we need to open a link in new tab using mouse right click selection, which we will be deciding at runtime:

enter image description here

<a ()="campaignLink(c, $event)" routerLink="[]">{{c.name}}</a>

Is there any way we can capture an open a-link new tab event so that we can call campaignLink function there?

CodePudding user response:

can use the "click" event to capture the click on the anchor tag and then use the "target" property of the event to check if it is a "_blank" target. If so, you can call the campaignLink function.

<a (click)="onClickLink($event)" routerLink="[]">{{c.name}}</a>

onClickLink(event) {
   if (event.target === '_blank') {
      this.campaignLink(c, event);
   }
}

CodePudding user response:

You can replace a tag a with span or something, and on the click event execute your piece of code and then navigate via router to another page.

Or you can still use a tag, call $event.preventDefault(), execute your piece of code, and then navigate via window or router.

  • Related