routerLink directive does not respect `event.preventDefault()` to prevent navigation
See original GitHub issueI’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
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:
- Created 6 years ago
- Reactions:41
- Comments:32 (12 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
i updated from 5.0.2 to 5.3.2 and now its working:
(click)="partcipate();$event.preventDefault();$event.stopPropagation()"
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.