Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

400
Vistas
Angular: Using (click) and routerLink on the same Button

I have a button that redirects to a new page and at the same time should save data to a Service. As I use it now it looks like this:

<button [disabled]="!isValid" (click)="saveToService()" routerLink="/link">Next</button>

Now I wonder if this is best practice. It feels like the html button is somewhat cluttered by so many seperate functionalities. The obvious alternative is to move the router navigation to a function that does both things, as in:

<button [disabled]="!isValid" (click)="saveAndNavigate()">Next</button> and in ts:

private saveAndNavigate():void { this.service.setData(data); this.router.navigate(['/link]); }

Is there a 'right' way to do this? Are there some unwanted side effects from doing both actions in html?

Thanks

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

I would suggest you to do it in router promises. So you can:

this.router.navigate(['/link]).then(() => {
 this.service.setData(data);
});
over 4 years ago · Santiago Trujillo Denunciar

0

I would implement the OnDestroy function in your component, so you can store the data when the component terminates.

Something like this in HTML:

<button [disabled]="!isValid" routerLink="/link">Next</button>

And like this in your component:

import { Component } from '@angular/core';

@Component({...})
export class ThisComponent implements OnDestroy {


    ngOnDestroy(){
        saveToService()
    }
}
over 4 years ago · Santiago Trujillo Denunciar

0

If your navigation is performed regardless of the outcome of the service call, then Fatih's answer would work just fine.

On the other hand, and what I've normally seen, is that page navigation should only occur after (successful) completion of the request. If this is the case, I would remove the routerLink directive from your button and keep the (click) function. That function could look like this:


// if your service is making an Http request
public saveToService() {
  this.service.saveStuff().pipe(
    tap(() => this.router.navigate(['/somewhere']))
  )
}

tap simply performs some action without affecting the data stream, so it's perfect for router navigation.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda