Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular is it possible to detect if query Paramter has changed?

Angular is it possible to detect if query Paramter has changed? I have a Component that handles 2 query paramters and according to what paramter you come with it sets some variables true or false. The Problem is if im on my component with queryparameter 1 the qp can change to the second paramter without leaving the component and in that case i need to set variables diffrently. So how do I detect that? Is that even possible?

like image 480
Kajot Avatar asked Dec 28 '17 07:12

Kajot


2 Answers

You can subscribe to the params in the root component

constructor(route:ActivatedRoute) {
    route.queryParams.subscribe(p => console.log(p.myQueryParam)); // you can also do this in ngOnInit
}

See also https://angular.io/api/router/ActivatedRoute

You can have query params on other route levels as well, but then they are called matrix parameters.

See also the end of this section https://angular.io/guide/router#route-parameters-required-or-optional

like image 129
Günter Zöchbauer Avatar answered Oct 20 '22 14:10

Günter Zöchbauer


You can subscribe to the params observable provided by ActivatedRoute module to observe changes in the query parameters.

constructor(private route:ActivatedRoute) {
    this.route.params.subscribe(
        (params:Params)=>{
            console.log(params['yourId']);
    };
)}
like image 44
Avinash Dangi Avatar answered Oct 20 '22 15:10

Avinash Dangi