router.navigate and ActivatedRoute
...
import { Router } from '@angular/router';
...
@Component({
selector: 'product-list',
template: `<div>
<ul>
<li *ngFor="let prod of products" (click)="gotoProdDetails(prod)">
<span>{{prod.id}} -- {{prod.name}}</span>
</li>
</ul>
</div>`
})
export class ProductListComponent {
products = [
{id: 1, name: "Milk"},
{id: 2, name: "Bananas"},
{id: 3, name: "Almonds"}
];
constructor(private router: Router){
}
gotoProdDetails(product){
router.naviagte(['/ProductDetails', product.id]);
// π is based on the assumption that '/ProductDetails' route is configured as below:
// { path: '/ProductDetails/:id', component: 'ProductDetailsComponent' }
}
}Reading Route param value in the component using ActivatedRoute
ActivatedRouteLast updated