Umut Arpat
Umut Arpat

Reputation: 566

Angular: How can I go to a different route when I click a button twice?

First I show the home page.

enter image description here

After I click the "Sayfaya git" button it shows the list page.

enter image description here

What I want to do is: When I click the button again it should go to the home page.

how can i do that ?

app.component.html

<div>
  <h1>Merhaba dünya</h1>
  <button [routerLink]="['/list']"  > Sayfaya git</button>
</div>

<div>
  <router-outlet></router-outlet>
</div>

Routes

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import {HomeComponent} from './home/home.component';
import {ListComponent} from './list/list.component';

const routes: Routes = [
{path:"",component: HomeComponent},
{path:"list",component: ListComponent},

];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

Upvotes: 1

Views: 210

Answers (1)

Chenna
Chenna

Reputation: 2623

Use dblclick

<button (click)="onSingleClickNavigate()" 
        (dblclick)="onDoubleClickNavigate($event)"
>Sayfaya git</button>
import { Router } from '@angular/router';

constructor(private router: Router) {}

onSingleClickNavigate() {
   this.router.navigate(['/list']);
}

onDoubleClickNavigate(event) {
   event?.preventDefault(); // Prevents single click behavior
   this.router.navigate(['/home']);
}

Upvotes: 2

Related Questions