I have a child component that is using route.snapshot.queryParamMap.get() to grab query parameters and pass that string to a service which in turn calls a function and populates my parent component. In the child component I have a search feature. After the user makes their selection and clicks search it fires a function that updates the query parameters of the URL. When the user clicks search I would like my parent component to update the data based on the new query parameters passed to the URL.
I've looked at a lot of posts, but I can't get my component to update consistently without hitting refresh. I'm subscribing to router.events. I've tried calling the function in both my constructor and in ngOnit. I see that I'm getting the NavigationEnd object back, but I'm not sure how to take the updated queryParameters to update my component.
How can I update my component without having to hit refresh?
// parent component
export class ResultsComponent implements OnInit {
results: Array<Entity>;
urlQuery: string;
entityConfig: any = {};
providers = PROVIDERS;
constructor(
private modelService: ModelService,
private entityService: EntityService,
private route: ActivatedRoute,
private router: Router
) {}
ngOnInit() {
this.urlQuery = this.route.snapshot.queryParamMap.get('entity_type');
this.entityService.getEntityType(this.urlQuery);
this.entityService.getEntities(this.entityService.entityConfig.endpoint)
.subscribe(
response => {
this.results = this.modelService.assignModelFromResponse(this.entityService.entityConfig.model, response);
console.log('results', this.results);
}
);
console.log('router events', !!this.router.events);
// how can I use router.events to update my component?
if(!!this.router.events) {
this.router.events.forEach((event) => {
if(event instanceof NavigationEnd) {
console.log('navigation end', event);
this.entityService.getEntityType(this.urlQuery);
}
// NavigationEnd
// NavigationCancel
// NavigationError
// RoutesRecognized
});
}
}
}