145

I am calling router.navigate on same page with some query string parameters. In this case, ngOnInit() does not call. Is it by default or do I need to add anything else ?

jb326
  • 1,345
  • 2
  • 14
  • 26
Jeeten Parmar
  • 5,568
  • 15
  • 62
  • 111

16 Answers16

194

You can inject the ActivatedRoute and subscribe to params

constructor(route:ActivatedRoute) {
  route.params.subscribe(val => {
    // put the code from `ngOnInit` here
  });
}

The router only destroys and recreates the component when it navigates to a different route. When only route params or query params are updated but the route is the same, the component won't be destroyed and recreated.

An alternative way to force the component to be recreated is to use a custom reuse strategy. See also Angular2 router 2.0.0 not reloading components when same url loaded with different parameters? (there doesn't seem to be much information available yet how to implement it)

Jota.Toledo
  • 27,293
  • 11
  • 59
  • 73
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
138

You could adjust the reuseStrategy on the Router.

constructor(private router: Router) {
    // override the route reuse strategy
    this.router.routeReuseStrategy.shouldReuseRoute = function() {
        return false;
    };
}
Pascal
  • 1,481
  • 1
  • 7
  • 2
  • 7
    This will initiate the ngInit of every component on the page. – jforjs Jun 04 '18 at 07:16
  • 3
    Well, @Pascal, you compelled me to log in to SO just to up-vote your answer. I would add that in Angular 6 you must also add `onSameUrlNavigation: 'reload'` to the config object like so: `RouterModule.forRoot(appRoutes, {onSameUrlNavigation: 'reload'})`. Coudn't speak for any other Angular versions, though. – Hildy Sep 19 '18 at 19:07
  • 2
    @Hildy, I was compelled to do the same :) Thanks @Pascal! if you prefer lambda you can do this this.router.routeReuseStrategy.shouldReuseRoute = () => false; – nick Oct 19 '18 at 15:31
  • @Pascal where should i add this entry? – Swaprks Dec 02 '18 at 08:02
  • 1
    @Swaprks i created a routing.module.ts and placed the code as this constructor – Pascal Dec 03 '18 at 09:15
  • maybe this should be the accepted answer; putting ngOnInit items in a subscription is counter intuitive – Nylon Smile Jun 06 '19 at 21:38
  • 2
    @Swaprks.. I have a similar issue and would like to try out your answer above. But it doesn't tell me much as a beginner in this field. Exactly where in the code am I supposed to place this code piece? Am I supposed to change anything in the code snippet (e.g. ´function()´ )? If you created a new file with the name routing.module.ts, how is it supposed to interact with other files then? There is also a file called app-routing.module.ts which is automatically created. – edn Oct 09 '19 at 12:41
  • still useful... I tried to call ngOnIt in angular 8 after navigation and was not able to call the same. after using this ia m now able to call ngOnIt. not sure whether this is the right approach in angular 8 or not BUT for now its fixing my issue. +1 for that. – patel.milanb May 19 '20 at 09:37
  • This worked for my case. My case is i have the url: /home/students/1 which has component showing information about that particular student with Id=1 and below a 2nd component that has a network graph with all of his siblings. In the nav bar, there's a search bar, i'm searching for a different student (let's say student with ID=2). Witth the proposed solution above, i'm forcing the creation of all the component (which the behavior that i want) and showing me all about student with ID = 2 – hamza saber Jul 13 '20 at 01:04
  • It baffles me that `/user/3` and `/user/5` are treated as **equal**. But maybe I got something wrong. – Chuck Feb 22 '21 at 08:08
41

Angular 9

I have used the following and it worked.

onButtonClick() {
    this.router.routeReuseStrategy.shouldReuseRoute = function () {
        return false;
    }
    this.router.onSameUrlNavigation = 'reload';
    this.router.navigate('/myroute', { queryParams: { index: 1 } });
}
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
Guru Cse
  • 2,805
  • 2
  • 18
  • 15
  • 2
    In addition, also use lambda instead of above, this.router.routeReuseStrategy.shouldReuseRoute = () => false; – Dhwanil Patel May 26 '20 at 06:11
  • Works in Angular 8. – Kishan Vaishnav Jun 15 '20 at 08:34
  • 17
    Would this change router behavior across the application or just trigger once for this specific `navigate()`? – Halfist Aug 18 '20 at 08:23
  • Works in Angular 9. – Ilija Iličić Nov 25 '20 at 10:54
  • 2
    @Halfist What I have observed is that after setting shouldReuseRoute, it is set for the entire app. I checked this by using console.log(this.router.routeReuseStrategy on page 1, setting the strategy to that function on page 2 and then navigating back to page 1. On initial load page 1 reports no function, but after navigating to page 2 and back the function still exists. If you only want to use this in 1 component, perhaps re-set the strategy in NgOnDestroy? – S. ten Brinke Jul 08 '21 at 12:40
  • 5
    Be careful with this!!! When that code executes it'll change the behavior of the router **permanently**. This means for example , if you use `router.navigate()` to the same url it'll trigger `ngOnInit` again. – Caveman May 04 '22 at 08:39
11

Do you probably need reloading page? This is my solution: I've changed the @NgModule (in app-routing.module.ts file in my case) :

@NgModule({
  imports: [RouterModule.forRoot(routes, {onSameUrlNavigation: 'reload'})] })
Dionis Oros
  • 664
  • 8
  • 12
  • What is the 'routes' over here. – Dhwanil Patel May 26 '20 at 04:34
  • @DhwanilPatel your Angular app routes. For example "const routes: Routes = [ { path: 'crisis-center', component: CrisisListComponent }, { path: 'heroes', component: HeroListComponent }, ]" https://angular.io/guide/router#register-router-and-routes – Dionis Oros Jun 03 '20 at 12:41
7

On your navigation method,

this.router.routeReuseStrategy.shouldReuseRoute = () => false;
this.router.onSameUrlNavigation = 'reload';
this.router.navigate(['/document'], {queryParams: {"search": currentSearch}});
Dhwanil Patel
  • 2,273
  • 1
  • 18
  • 28
  • 2
    this destroy the rest of the navigation model – Shaybc Dec 02 '21 at 10:24
  • 2
    Be careful with this!!! When that code executes it'll change the behavior of the router **permanently**. This means for example , if you use `router.navigate()` to the same url it'll trigger `ngOnInit` again. – Caveman May 04 '22 at 08:41
  • Yes, Developer have to manage relevant things accordingly. – Dhwanil Patel May 04 '22 at 10:25
5

Here is a collection of the best ideas on this page with more information

Solution 1 - Use params subscription:

Tutorial: https://angular-2-training-book.rangle.io/routing/routeparams#reading-route-parameters

Docs: https://angular.io/api/router/ActivatedRoute#params

In each of your routing components that use param variables include the following:

import { Component, OnInit, OnDestroy } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { Subscription } from 'rxjs';

// ...

@Component({
    // ...
})
export class MyComponent implements OnInit, OnDestroy {
    paramsSub: Subscription;

    // ...

    constructor(activeRoute: ActivatedRoute) {

    }

    public ngOnInit(): void {
        // ...
        this.paramsSub = this.activeRoute.params.subscribe(val => {
            // Handle param values here
        });

        // ...
    }

    // ...

    public ngOnDestroy(): void {
        // Prevent memory leaks
        this.paramsSub.unsubscribe();
    }
}

Some common issues with this code is that subscriptions are asynchronous and can be trickier to deal with. Also you can't forget to unsubscribe on ngOnDestroy or else bad things can happen.

Good thing is that this is the most documented and common way to handle this problem. There's also a performance improvement doing it this way since you are reusing the template instead of destroying and recreating each time you visit a page.

Solution 2 - shouldReuseRoute / onSameUrlNavigation:

Docs: https://angular.io/api/router/ExtraOptions#onSameUrlNavigation

Docs: https://angular.io/api/router/RouteReuseStrategy#shouldReuseRoute

Docs: https://angular.io/api/router/ActivatedRouteSnapshot#params

Find where RouterModule.forRoot is located in your project (normally found in app-routing.module.ts or app.module.ts):

const routes: Routes = [
   // ...
];

// ...

@NgModule({
    imports: [RouterModule.forRoot(routes, {
        onSameUrlNavigation: 'reload'
    })],
    exports: [RouterModule]
})

Then in AppComponent add the following:

import { Component, OnInit} from '@angular/core';
import { Router } from '@angular/router';

// ...
@Component({
    // ...
})
export class AppComponent implements OnInit {
    constructor(private router: Router) {
    }

    ngOnInit() {
        // Allows for ngOnInit to be called on routing to the same routing Component since we will never reuse a route
        this.router.routeReuseStrategy.shouldReuseRoute = function() {
            return false;
        };

        // ...
    }

    // ...
}

Last, in your routing components you can now handle param variables like this:

import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';

// ...

@Component({
    // ...
})
export class MyComponent implements OnInit {
    // ...

    constructor(activeRoute: ActivatedRoute) {

    }

    public ngOnInit(): void {
        // Handle params
        const params = +this.activeRoute.snapshot.params;

        // ...
    }

    // ...
}

Common issues with this solution is that it isn't common. Also you are changing the default behavior of the Angular framework, so you can run into issues people wouldn't normally run into.

Good thing is that all your code is synchronous and easier to understand.

Mark Thompson
  • 448
  • 5
  • 7
1

NgOnInit would be called once when an instance is created. For the same instance NgOnInit won't be called again. In order to call it it is necessary to destroy the created instance.

micronyks
  • 54,797
  • 15
  • 112
  • 146
1

I've had the same issue, additionally I got the warning:

did you forget to call `ngZone.run()`

This site provided the best solution:

import { Router } from '@angular/router';
import { NgZone } from '@angular/core';

...

  constructor(
    private ngZone:NgZone,
    private _router: Router
  ){ }

  redirect(to) {
    // call with ngZone, so that ngOnOnit of component is called
    this.ngZone.run(()=>this._router.navigate([to]));
  }
RobC
  • 22,977
  • 20
  • 73
  • 80
XnyXno
  • 19
  • 3
  • With respect to this, I wish to know how to handle the issue when you simply refresh the page with the new route. In that case, the NgZone warning will still be present. – Siddhant Sep 15 '20 at 07:55
1

The solution could be subscribing to your router events.

Import Router and NavigationEnd

import { Router, NavigationEnd } from '@angular/router';

Initiate router in the constructor.

constructor(router: Router) {

Subscribe to router events in the constructor.

        this.router.events.subscribe((ev) => {
          if (ev instanceof NavigationEnd) {
            //do something
          }
        });

I found out developers sometimes need generic code for every router.navigate, this can be the answer if it's in app.component.ts or in any shared component.

Bedair
  • 420
  • 4
  • 12
0

This problem is likely coming from that fact that you are not terminating your subscriptions using ngOnDestroy. Here is how to get'ter done.

  1. Bring in the following rxjs subscription import. import { Subscription } from 'rxjs/Subscription';

  2. Add OnDestory to your Angular Core Import. import { Component, OnDestroy, OnInit } from '@angular/core';

  3. Add OnDestory to your export class. export class DisplayComponent implements OnInit, OnDestroy {

  4. Create a object property with a value of Subscription from rxjs under your export class for each subscription on the component. myVariable: Subscription;

  5. Set the value of your subscription to MyVariable: Subscriptions. this.myVariable = this.rmanagerService.getRPDoc(books[i].books.id).subscribe(value => {});

  6. Then right below ngOninit place the ngOnDestory() life cycle hook and put in your unsubscribe statement for your subscription. If you have multiple, add more ngOnDestroy() { this.myVariable.unsubscribe(); }

Steve Klock
  • 115
  • 1
  • 8
0

Create a different path for the same component in the routes array .

const routes : Routes = [ { path : "app", component: MyComponent }, { path: "app-reload", component: MyComponent }];

If the current URL is "app" then navigate using "app-reload" and vice versa.

S A
  • 187
  • 1
  • 2
  • 7
0
const defaultStrategy = this.router.routeReuseStrategy.shouldReuseRoute;
this.router.routeReuseStrategy.shouldReuseRoute = () => false;
this.router.navigate(['path/to']).finally(
() => {this.router.routeReuseStrategy.shouldReuseRoute = defaultStrategy;})
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • 1
    Although this code might answer the question, I recommend that you also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes. – Mark Rotteveel Jun 03 '23 at 09:13
-1

Consider moving the code you had in ngOnInit into ngAfterViewInit. The latter seems to be called on router navigation and should help you in this case.

Manoj Amalraj
  • 535
  • 1
  • 5
  • 14
-1

This is the best solution for Router Navigate does not call the ngOnInit function when same page

// override the route reuse strategy
this.router.routeReuseStrategy.shouldReuseRoute = function() {
    return false;
};
Joundill
  • 6,828
  • 12
  • 36
  • 50
-1
// this code is for redirecting to the dashboard page with calling ngOnInIt
    this.router.routeReuseStrategy.shouldReuseRoute = () => false;
    this.router.onSameUrlNavigation = 'reload';
    this.router.navigate(['./dashboard']);

this should works and should only used for special cases. When you dont have to navigate much after redirection you can use this.otherwise this will messed up some times. Note that you can always make the 'routeReuseStrategy' to true when you needed.

Md omer arafat
  • 398
  • 7
  • 10
-9

When you want to router navigate on the same page and want to call ngOnInit(), so you do like that e.g,

this.router.navigate(['category/list', category]) .then(() => window.location.reload());