Matt Fazzini
Matt Fazzini

Reputation: 151

Angular2- Routing to new component in typescript

I am trying to run a function when a button is clicked that will prep a variable and then at the end of the function redirect to another component and pass that variable. (I want functionality similar to the RouterLink in HTML but from the typescript) I already know how to get the function to run and prep the variable but do not know how to send that variable to a new route (component) from the typescript.

Upvotes: 1

Views: 2009

Answers (1)

Tiago Bértolo
Tiago Bértolo

Reputation: 4333

  1. Add (click)="myFunction()" on your component HTML.

  2. Import the Angular Router service in your component: import { Router } from '@angular/router';

  3. Inject the router in your component constructor like this: constructor( private router: Router, ...) { }

  4. And then define myFunction such as:

    myFunction(){
      this.router.navigate(['/myPath']);
    }
    

Upvotes: 4

Related Questions