I am seeking to replicate the traditional functionality found in HTML where clicking a link triggers a confirmation pop-up before being redirected:
<a href='http://www.example.com' onclick="if ( window.confirm ('Are you sure you want to go there?') === false ) { return false; } ">goto example.com</a>
If the user clicks cancel, the browser remains on the current page. Simple and effective.
But how can this be achieved using Angular-based HTML? It seems the typical approach doesn't quite work:
<a routerLink='/go-there'> go </a>
As we've seen, adding an onclick event like above does not produce the desired result.
<a routerLink='/go-there' onclick="if ( window.confirm ('Are you sure you want to go there?') === false ) { return false; } "> go </a>
Currently, I'm handling this in the .ts file while still utilizing routerLink, as shown below:
const user_approval = window.confirm ('Are you sure you want to go there?');
if ( !user_approval ) {
alert('Action is cancelled. \nYou are not going there.');
return false;
}
This method works but only after the routerLink has already been activated, which could have been avoided.
So, how can we prevent the routerLink destination from being accessed if the user changes their mind about proceeding?