I am trying to route to the current page with different param with no avail. I have a combo box that triggers:
this.router.navigate(['page', selectedId]);
the Url is changing but that's about it.
How do I route to the same page with different param?
You can navigate one to another page in Angular in Two ways. (both are same at wrapper level but the implementation from our side bit diff so.) routerLink directive gives you absolute path match like navigateByUrl() of Router class.
navigate method, you must supply the ActivatedRoute to give the router knowledge of where you are in the current route tree. After the link parameters array, add an object with a relativeTo property set to the ActivatedRoute . The router then calculates the target URL based on the active route's location.
Angular router traverses the URL tree and matches the URL segments against the paths configured in the router configuration. If a URL segment matches the path of a route, the route's child routes are matched against the remaining URL segments until all URL segments are matched.
The route parameters are required and is used by Angular Router to determine the route. They are part of the route definition. For Example, when we define the route as shown below, the id is the route parameter. The above route matches the following URL The angular maps the values 1 & 2 to the id field
The Angular preserves or carry forwards the query parameter of the current route to next navigation. Any query parameters of the next route are discarded The Angular merges the query parameters from the current route with that of next route before navigating to the next route.
Using Angular router we can navigate from one view to next while performing our task. Every component can be mapped with a URL and when that URL is accessed by browser or by a link on the page then the corresponding component is displayed. We can pass optional parameter with the URL that will be fetched in component to filter data to display.
The ActivatedRoute is a service, which keeps track of the currently activated route associated with the loaded Component. The Angular adds the map all the route parameters in the ParamMap object, which can be accessed from the ActivatedRoute service The ParamMap makes it easier to work with parameters.
The page will not refresh, cause that's how the Router
works.. without refreshing the page!
You have to subscribe to the route parameters to detect changes in them and then do whatever you want to with that information..
So inject ActivatedRoute
into your component and subscribe to the parameters like this:
constructor(private route: ActivatedRoute) {}
ngOnInit() {
this.route.params.subscribe(params => {
// PARAMS CHANGED .. TO SOMETHING REALLY COOL HERE ..
// for example extract the id..
let id = +params['id']; // (+) converts string 'id' to a number
});
}
Ok, after reading all your answers specially @mxii's (thank you for this), I understand that:
Good Lesson, Thanks!
you can do this by using this
this.router.navigateByUrl('page_url/' + your_id);
By doing so your params will change successfully.
But it will only change your URL. If you really want to load some methods then you have to call them manually.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With