0

The app has pages, X, Y and Z. So the routing should be as follows I go into page X and then select some details and then go to page Y and then select some more details and then go to Z. So I want that after going to Z page once I click window back button it should go to page X and not page Y. While going from Y to Z, I also tried adding { location: 'replace' } in $state.go but it doesn't work. Any ideas on how to achieve that ?

Thanks

abkds
  • 1,764
  • 7
  • 27
  • 43

2 Answers2

3

Every time you change a state, Angular will trigger an event called $stateChangeStart, you can use that to your advantage.

In your app.run, do

app.run(function($rootScope,$state){
    $rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams) {
        //Check if you're going from Z to Y
        if(fromState.name === 'Z' && toState.name === 'Y'){ 
            // if so, first stop the default behavior
            event.preventDefault();
           // then navigate to X
            $state.go('X');
        }
    });
});

The browser back button essentially triggers the same event so your problem will be solved with this.

AdityaParab
  • 7,024
  • 3
  • 27
  • 40
0

do like this give your state in $state.go like.. : $state.go('y')

Ankur Soni
  • 746
  • 6
  • 17