2

I have an angular component that for the purpose looks like this:

<div click="onClick()">
<a [routerLink]="url">
</a>
</div>

<a> occupies 100% of the div, so when I am clicking the div or anchor, I want to trigger onClick() event plus navigate.

Problem is sometimes the navigation occurs first and the click event is lost. Since I want to on hover display the url link where I'm navigating too, I think there isn't other solution besides using routerLink is there?

Basically to show this:

enter image description here

And if not, how could I make sure that despite navigating, I always trigger onClick()?

3
  • 1
    i am making a stackblitz example for you , one moment please Commented Dec 16, 2020 at 18:22
  • @DolevDublon thanks Commented Dec 16, 2020 at 18:37
  • 1
    hey the stackblitz example doesnt work properly because stackblitz work diffrent than the local programming envierment so i will mention the steps for you to take in order to make stuff work for you ! Commented Dec 16, 2020 at 18:54

3 Answers 3

4

You can inject the Router service inside your Component Controller and then set the navigation inside your onClick function.

constructor(private readonly router: Router) { }

and then:

onClick() {
     this.router.navigate(['url']);
    ...
}
Sign up to request clarification or add additional context in comments.

1 Comment

but that way I will loose the on hover url I think that browser shows
4

what's up!

a couple of words before we get to the actual answer just to make sure you know how to work with router module

const routes: Routes = [
  { path: "", pathMatch: "full", redirectTo: "home" },
  { path: "home", component: HomepageComponent },
  { path: "register", component: RegisterComponent },
  { path: "buyingmainpage", component: BuyingmainpageComponent , canActivate : [AuthGuard] },
  { path: "checkoutmainpage", component: CheckoutmainpageComponent , canActivate : [AuthGuard] },
  { path: "checkoutsuccess", component: CheckoutsuccessComponent , canActivate : [AuthGuard] },
  { path: "**", component: Eror404Component },
];
 
@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})


tell me if this is familiar concept you are familiar with ?

the first step is to include the path of the url you want in the routes like the code above

the answer

the way to make this work in angular is by using Router

first you import it in the beginning of the code

import { Router } from '@angular/router';

then on the constructor function you declare it like this

  constructor(
    private router: Router,
    ) {}

then you can run the onClick function you wanted and use the function

router.navigateByUrl("/abc")

with a string inside it to navigate to the url you want :)

onClick(){
this.router.navigateByUrl("/checkoutmainpage")
}

if you want a video call i can help you ,

feel free to contact me on whats app or email , whats app will be quiker

[email protected]

1 Comment

I'm familiar with that, but I want to the url popover that routerlink adds. Using router it won't show up.
0

The Click event will occur before the url href fires (routerLink). What you want to do is make sure one happens and not the other by preventing the event to bubble up. We do the same in our app to maintain browser "history" on visited links, but let the click event actually do the routing, as there is some additional setup we need to do prior to navigating.

<div>
  <!-- ie: <a href="the-path-to-my-route"></a> -->
  <a click="onClick(); $event.preventDefault()" [routerLink]="url">
  </a>
</div>

The above will case the click event to be handled, and the actual element to display an anchor href with full url.

then in your ts

onClick() {
  this._router.navigate([url]);
}

Just make sure your route url path is valid.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.