I have an Angular 7 page-component that contains another component. When selecting a product from select box on the little component, it navigates to another page with the same page-component (ProductDetails), but with different parameters in the url (different productId).
The navigate on the little component when selecting a product:
onProductSelected(args) {
const selectedOption = args.target.value;
this.router.navigate(['/products', selectedOption]);
}
page-component ProductDetails.ts:
@Component({
selector: 'product-details',
templateUrl: './product-details.component.html',
styleUrls: ['./product-details.component.scss']
})
export class ProductDetailsComponent implements OnInit, OnDestroy {
currentProductId: string;
product: ProductModel;
subscriber;
constructor(private productService: ProductService,
private route: ActivatedRoute) {}
ngOnInit() {
this.currentProductId = _.get(this.route, 'snapshot.params.productId');
this.subscriber = this.productService.getProdactById(this.currentProductId)
.subscribe(res => {
this.product = res;
});
}
ngOnDestroy() {
if (this.subscriber) {
this.subscriber.unsubscribe();
}
}
}
I need the ngOnInit of the page-component to notice that the url changed and re-create itself. (in order to get the new productId parameter and render the product details).
How can I do that?
Thanks!