Search code examples
angularangular-router

Angular navigation problem with the ngOnInit method of the previous page


I have two pages A and B and navigation takes place from page A to page B.
On page B I inserted a button to go back:

import { Location } from '@angular/common';
...
this.location.back();  

Navigation works correctly, but I don't want the ngOnInit method in page A to be invoked again when I go back.
This is the method I use to navigate between the two pages:

this.router.navigate(['/pages/page-b', item_id]);

UPDATE:

I would not want the xxx method to be called because it makes database calls which in this case I would like to avoid


Solution

  • There is nothing much to be needed here. When you visit the ngOnInit() for the first time. You should turn the variable true, by declaring one more variable.

    When you come back from page B. In your ngOnInit() just check the value of that variable. If its true, don't call the method used for api call.