Home > Enterprise >  Angular: Click button to redirect to another page not working
Angular: Click button to redirect to another page not working

Time:02-08

In the Market > Value page, I have a button named add, when the user clicks on this button, the user goes to another page. This page is named ajout.

Here is the structure

structure

value.component.html

<div >
    <button type="submit"  (click)="btnAjout">Add</button>
</div>

value.component.ts

btnAjout(): void {
   this.router.navigateByUrl('/ajout');
}

market-routing.module.ts

{
  path: 'value',
  component: ValueComponent,
},
{
  path: 'ajout', 
  component: AjoutComponent
},

ajout.component.html

<p>ajout works!</p>

The ajout page is not working, did I make a mistake?

Code Stackblitz

CodePudding user response:

Issue 1: Unable fire btnAjout method

btnAjout is a function. Hence with (click)="btnAjout", it never trigger the btnAjout function.

<button type="submit"  (click)="btnAjout">Add</button>
btnAjout(): void {
  ...
}

Solution for Issue 1

Correct the click event.

<button type="submit"  (click)="btnAjout()">Add</button>

Issue 2: Incorrect routing to 'market/ajout'

From reviewing DashboardRoutingModule to MarketingRoutingModule, (to redirect) adjout component's URL should be:

/market/adjout

Instead of:

this.router.navigateByUrl('/ajout');

Solution for Issue 2

Correct the url path.

this.router.navigateByUrl('/market/ajout');

Sample Solution on StackBlitz

  •  Tags:  
  • Related