Using Router -> getCurrentNavigation () // My variable is always the same

Hello, I do not know how to ask my question but I use

this.router.navigateByUrl('tabs/list', {state: {country: this.country}});

I get my ‘country’ variable in my ‘list’ page very well, except that when I restart a query with another value in my variable.
When I get my variable in the ‘list’ page, like that :

this.stateUrl = this.router.getCurrentNavigation().extras.state.country;

This.stateUrl always returns me the value of my first variable …
What approaches are possible?
Reload my function? How?

Another way to get my variable?

Thanks !

Edit :
To put in context, I have a search bar, the user writes for example: “Paris”, then validates his search, he finds himself on the page ‘list’ with the name of several restaurants in Paris. If the user now returns to search and writes: “fksgrfs”, and validates, he will still have the results for Paris since the variable this.country does not update :disappointed:

Hi, I used this code for parsing variable to another page:

this.router.navigate(['/something', { value: this.data }]);

make sure to import router from ‘@angular/router’;

For receiving value in another page I used this code:

import { ActivatedRoute, Router } from '@angular/router';
....................
value: string;
constructor(private router: Router, private route: ActivatedRoute) { 
   this.route.params.subscribe(params => {
           this.value = params['value'];
           console.log(this.value);
   });
}

In case you want to pass whole value, I’mean Array or something like that, Use this code for passing:

this.router.navigate(['/something'], {
             queryParams: {
                 value: JSON.stringify(value)
             }
         });

For receiving whole array value in another page I used this code:

import { ActivatedRoute, Router } from '@angular/router';
....................
value: any = {};
constructor(private router: Router, private route: ActivatedRoute ) { 
   this.route.queryParams.subscribe((res) => {
             this.value = JSON.parse(res.value);
         });
}

Is manual JSON stringify/parse really necessary there? I would find that surprising.

As I know it is necessary there. In case you don’t pass and get the value without JSON Object. you will the get the array or whatever you pass like this:
Screenshot_2
and in case you pass and get the value with JSON Object, you will the get the array or whatever you pass like this:
Screenshot_3

1 Like

Thanks, that’s interesting. So in your experience there would seem to be a difference between deeply-nested objects and, for example, a { [k: string]: string } or a number[].

Is your search control on the same page as your list, or a different page?