Below code describes routes in my angular 2 application
RouterModule.forRoot( [
{path:'' , component:HomeComponent},
{path:'acategories/:id/products/:pid' , component:ProductComponent},
{path:'acategories/:id/products' , component:ProductsComponent},
{path:'acart' , component:CartComponent},
{path:'about' , component:AboutComponent},
{path:'aorders' , component:OrderDetailsComponent},
{path:'asearch' , component: ProductSearchComponent},
{path:'**',component:PageNotFoundComponent}
])
My root component has links to these routes as depicted in below image
User searches for an item by entering text in textbox and clicking search button.Once the user clicks "search" , below method in root component will be executed , which basically navigates to "asearch" route.
onSearch()
{
if(this.formGroup.valid)
{
this.router.navigate(['asearch' , {search:this.formGroup.controls['search'].value}]);
}
}
Now the issue I am facing is , when "asearch" route is already active[ ie its the current active route"] and user tries to search for an item , results are not displayed.
Below is the the code in ProductSearchComponent which gets the results from server.
ngOnInit() {
console.log('ngOnInit()');
this.route.params.subscribe( (params) => {
this.search = params['search'];
})
this.service.searchProducts(this.search).subscribe( {
next: (data) => {
this.products = data;
},
error: (err) => { this.toaster.error(err) }
})
}