1

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
          });
    }

}

}
3
  • That's quite a lot of clutter. Can you create a minimal example which highlights what you want to achieve and post it on StackBlitz so we can play around with it? Commented Sep 27, 2018 at 17:21
  • That would require a lot of overhead. I updated my code above. Basically I just need to know how can I use router.events to update my component? Commented Sep 27, 2018 at 17:31
  • 1
    It would certainly be more overhead for us to create the repro and figure it out. Commented Sep 27, 2018 at 17:32

2 Answers 2

3

Instead of using the snapshot, subscribe to this.route.queryParamMap. It is an Observable and you should receive the updated values when the route changes. https://angular.io/api/router/ActivatedRoute#queryParamMap

Sign up to request clarification or add additional context in comments.

Comments

0

I still need to use snapshot, but subscribing to the router.events solved the issue.

    ngOnInit() {
        this.urlQuery = this.route.snapshot.queryParamMap.get('entity_type');
        this.getEntity();

        this.router.events.subscribe((val) => {
            if(val instanceof NavigationEnd) {
                this.urlQuery = this.route.snapshot.queryParamMap.get('entity_type');
                this.getEntity();
            }
        });
    }

    getEntity():void {
        this.entityService.getEntityType(this.urlQuery);
        this.entityService.getEntities(this.entityService.entityConfig.endpoint)
        .subscribe(
            response => {
                this.results = this.modelService.assignModelFromResponse(this.entityService.entityConfig.model, response);

            }
        );
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.