1

I am using angular UI router with my app which has html5 routing (angular's html5 mode) enabled.

I navigate from one state to another mostly using ui-sref and it works fine. However, at times when I use the browser back button, nothing happens. Please note, this is a one of event. When it does work though, when I try going "forward" with my browser controls, nothing happens and I remain on the same state. All my states have unique urls. I am sort of stuck with this issue and unable to proceed. Any pointers would be helpful.

This is the way I set my application base path:

<base href="/" target="_blank"> 

This is what my router setup looks like:

$stateProvider
.state("base",{
    abstract: true,
    url: "/app",
    template: "<div ui-view></div>"
})
.state("login", {
    url: "/login",
    parent: "base",
    templateUrl: "somePath",
    controller: "loginController"
})
.state("auth",{
    abstract: true,
    parent: "base"
    resolve: {
        user: function(StateService){
            return StateService.getCurrentUser();
        }
    }
})
.state("state1", {
    url: "/state1",
    parent: "auth",
    templateUrl: "somePath",
    controller: "state1Controller"
})
.state("state2", {
    url: "/state2",
    parent: "auth",
    templateUrl: "somePath",
    controller: "state2Controller"
});

I essentially have some base state where I resolve some things and add a base path to rest of my sub states. I then have another abstract state where I resolve my current user. It has many child states, each can only be accessed by a logged in user.

The StateService has a function getCurrentUser that makes a request to get details of the current user from my backend. My backend maintains a session which has the details of the current user. If there is no user in the session, it returns a 401. To this, I redirect my user to the login page. If not, I just continue and the UI router takes care of the rest. For now, let us assume I don't store this user on a cookie, so if the page is refreshed, the request goes again to my backend.

I have a few interceptors as well. They listen to the $stateChangeStart event for some other application logic. For example, if the user is already logged in and he/she tries to go to the login page, I stop the state change and redirect the user to some default state (lets say state1). These are registered in the run block of my app.

$rootScope.$on("$stateChangeStart", function(event, toState, toParams, fromState) {
  if(toState && toState.url =="/login"){
    StateService.getCurrentUser()
    .then(function(user){
      $state.go("state1")
    })
    .catch(function(){
      // no problem, go ahead
    }); 
  }
});

At this point I am not sure what else code to provide, let me know if anything else is required. I think there could be some problem with the way state changes are stored in the browser history. If that can happen, please let me know how so that I can figure our what part of my code is causing it.

Rajan
  • 85
  • 9
  • need to provide some code. probably your ui-router setup – Austin Mar 30 '16 at 18:07
  • @Austin have added some. Let me know if anything else is required. – Rajan Mar 31 '16 at 03:36
  • This is a known challenge with Angular, and the solutions are pretty ugly. I have yet to find a good fix, although I'm passing state parameters, which makes the problem even more difficult to solve. – James Gentes Mar 31 '16 at 15:50
  • Updating this for the benefit of others. I never found any resolution to this issue, so I reverted back to non HTML5 mode routing, which magically fixes the history issues. – Rajan Oct 17 '16 at 06:43

1 Answers1

0

Since you are not providing any code i cannot be sure if you already tried this, but maybe THIS answer can help you

Community
  • 1
  • 1
ShinyDarkStone
  • 70
  • 1
  • 10
  • @ShinyDarkSone I have added some code, let me know if you need any more. Also, I did go through the answer you have linked, but I don't understand why I need it. As long as the URL in my browser is correct, the router should load up the correct state. Right? – Rajan Mar 31 '16 at 03:45
  • yes it should, but we need to see what is happening, if you put a break point in `$rootScope.$on("$stateChangeStart"` and look inside the toState, it should have the value from the last state, if not, and it have the same value check the fromState, maybe there is some routing problems we are not seeing. If you already tryed this can you comment me the results? – ShinyDarkStone Mar 31 '16 at 14:06