Changing state without changing browser history in angular ui-router

ぃ、小莉子 提交于 2019-12-10 01:08:45

问题


Assume that we have a logic like this:

  • From state A, change to state B.
  • Whenever we arrive to state B, the app always redirect us to state C by calling $state.go(stateC)
  • Now we are in state C

My question is how to go back to state A from state C (given the fact that state A can be any state we don't know at run-time, meaning user can access state B from any other states)


回答1:


Use the location option with value "replace"...

$state.go(stateC, null, {
    location: 'replace'
})

See https://angular-ui.github.io/ui-router/site/#/api/ui.router.state.$state#methods_go

location - {boolean=true|string=} - If true will update the url in the location bar, if false will not. If string, must be "replace", which will update url and also replace last history record.




回答2:


You could keep track of visited states in a service and then just call $state.go on the previous state.

You can watch the state changes like so:

$rootScope.$on('$stateChangeStart', 
function(event, toState, toParams, fromState, fromParams){ 
    // add fromState to history 
});

How to $watch state change of $stateProvider in AngularJS?



来源:https://stackoverflow.com/questions/39461556/changing-state-without-changing-browser-history-in-angular-ui-router

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!