I have these two route definitions, product list and product detail:
const routes: Routes = [
...
{
path: 'product',
component: ProductListComponent,
resolve: {
products: ProductListResolverService
},
children: [
{
path: ':productcode',
component: ProductDetailComponent,
resolve: {
product: ProductDetailResolverService
}
},
...
]
}
...
Inside the product list component I have a set of radio buttons to indicate which product is selected, while the router outlet displays the product detail component:
<div *ngFor="let product of Products" class="radio">
<label>
<input [(ngModel)]="SelectedProduct"
[value]="product"
(change)="RefreshDetail()"
name="selectedProduct"
type="radio">
{{product.Name}} {{product.Code}}
</label>
</div>
<router-outlet></router-outlet>
When the user clicks the radio button, the URL, the product detail component, and the radio button are synchronized correctly.
But when the user goes directly to a certain product URL, the URL and product detail component display the correct product, but the radio button doesn't. What I mean by going directly to a certain product URL is the user inputs the URL to the browser address bar and press enter.
I tried to get the value of :productcode from ActivatedRoute but it didn't work:
this.route.paramMap.subscribe(params => {
let productcode = params.get("productcode");
let product = this.Products.find(product => product.Code == productcode);
if (!!product) {
this.SelectedProduct = product;
}
});
The paramMap.get("productcode") is always null. Probably because it is not available in the product list route, but in the child route, the product detail route.
I haven't found a way to subscribe to product detail route from product list component. Besides product detail route is not the only child of product list route.
How do I subscribe to a specific child route parameter?