question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

routerLink directive does not respect `event.preventDefault()` to prevent navigation

See original GitHub issue

I’m submitting a…


[ ] Regression (a behavior that used to work and stopped working in a new release)
[x] Bug report  
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior

Attempting to prevent navigation for an anchor with a routerLink by calling event.preventDefault() on the click event has no effect.

Expected behavior

When event.preventDefault() is called on the click event, navigation should be prevented.

Minimal reproduction of the problem with instructions

Link to Repro

Clicking on “catalog” still navigates to the catalog view, event though the <a> tag contains (click)="$event.preventDefault()

What is the motivation / use case for changing the behavior?

Environment

Angular version: 5.2.0

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:41
  • Comments:32 (12 by maintainers)

github_iconTop GitHub Comments

17reactions
hansih91commented, Feb 6, 2018

i updated from 5.0.2 to 5.3.2 and now its working: (click)="partcipate();$event.preventDefault();$event.stopPropagation()"

14reactions
djmarquettecommented, Mar 4, 2020

We have a need to allow an HTML page to open a routerLink in a new tab via right-click context menu. However, when user clicks in a normal case we have a component click event that builds an object and passes it up a couple parent levels to open in a side-panel. I am trying to intercept the default behavior of the routerLink using preventDefault() in an ‘anchor-tag-directive’ we rolled but am not having success. Currently the link still opens in the current page, the default behavior for the routerLink tag.

I hope this helps explain why one might want to have a preventDefault() for a routerLink on a clickable element.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to prevent default click behavior of a routerlink in angular ...
The reason routerLink does not respect preventDefault is described here. The solution is to create inner element for anchor if there already ...
Read more >
Router link does not respect preventDefault (forked) - StackBlitz
onClick(event: MouseEvent): void {. console.log('should receive every click. event');. } ... Use of this source code is governed by an. MIT-style license that....
Read more >
Dropdown | Components - BootstrapVue
The dropdown menu can either be left aligned (default) or right aligned with respect to the button above it. To have the dropdown...
Read more >
RouterLinkActive - Angular
@Input() routerLinkActiveOptions: { exact: boolean; } | IsActiveMatchOptions. Options to configure how to determine if the router link is active. These options ...
Read more >
Angular 5 anchor link - 免费编程教程
routerLink directive does not respect `event.preventDefault()` to , <a href="">My Link</a> would do nothing in Angular 1.x, but it navigates to the app...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found