Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2 router - Navigate to the current page with different parameters

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?

like image 320
Avi Avatar asked Sep 21 '16 09:09

Avi


People also ask

How do you route from one page to another page in Angular?

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.

What does a router navigate do?

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.

How does Angular router navigate work?

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.

What are route parameters in angular router?

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

What is the difference between current route and next route in angular?

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.

How to navigate from one view to next using angular router?

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.

What is activatedroute in Angular 2?

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.


3 Answers

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

   });
}
like image 147
slaesh Avatar answered Oct 18 '22 04:10

slaesh


Ok, after reading all your answers specially @mxii's (thank you for this), I understand that:

  1. when routing to the same page with different param, only the param changes. the DOM stand still, no component rebuild, no onInit cycle.
  2. The OnChanges hook triggers on every data-bind changes, which mean if I pass the param to child component I can't use OnInit on with this param, I must use OnChanges hook.

Good Lesson, Thanks!

like image 7
Avi Avatar answered Oct 18 '22 03:10

Avi


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.

like image 4
Pardeep Jain Avatar answered Oct 18 '22 04:10

Pardeep Jain