Last active
August 21, 2023 07:42
-
-
Save LidiaKovac/eb623b966e7f3c9033ad4787eb614053 to your computer and use it in GitHub Desktop.
ROUTE PARAMETER // Angular
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component, OnInit } from '@angular/core'; | |
import { ActivatedRoute, Router } from '@angular/router'; | |
@Component({ | |
templateUrl: './details.component.html', | |
styleUrls: ['./details.component.scss'] | |
}) | |
export class DetailsComponent implements OnInit { | |
id!: string | |
constructor(private route: ActivatedRoute, private router: Router) { | |
} | |
ngOnInit() { | |
this.route.paramMap.subscribe(params => this.id = params.get("id")!) | |
//Dalla rotta correntemente attiva ci iscriviamo ai parametri e e salviamo nella variabile id li parametro in tempo reale. | |
//se non aveste chiamato il parametro "id", il parametro di .get() avra' il nome del parametro che avete scelto. | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const routes: Routes = [{ | |
path: ":id", //i due punti indicano un parametro DINAMICO. Ovviamente potete cambiare sia la rotta base che il nome del parametro, | |
//esempio: /profile/:userId, oppure /pippo/:paperino. L'importante e' che nei passaggi successivi utilizziate il nome corretto. | |
component: DetailsComponent | |
}, | |
{ | |
path: "", | |
component: HomeComponent | |
} | |
]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment