有没有办法获得当前状态的前一个状态?
例如,我想知道当前状态B之前的前一个状态是什么(其中前一个状态应该是状态A)。
我在ui-router github文档页面中找不到它。
发布于 2013-05-28 23:40:07
ui- $stateChangeSuccess
一旦转换就不会跟踪前一个状态,但是当状态改变时,事件路由器就会在$rootScope
上广播。
您应该能够从该事件捕获前一个状态(from
是您要离开的状态):
$rootScope.$on('$stateChangeSuccess', function (ev, to, toParams, from, fromParams) {
//assign the "from" parameter to something
});
发布于 2014-09-20 11:51:52
在移动到新状态之前,我使用resolve保存当前状态数据:
angular.module('MyModule')
.config(['$stateProvider', function ($stateProvider) {
$stateProvider
.state('mystate', {
templateUrl: 'mytemplate.html',
controller: ["PreviousState", function (PreviousState) {
if (PreviousState.Name == "mystate") {
// ...
}
}],
resolve: {
PreviousState: ["$state", function ($state) {
var currentStateData = {
Name: $state.current.name,
Params: angular.copy($state.params),
URL: $state.href($state.current.name, $state.params)
};
return currentStateData;
}]
}
});
}]);
发布于 2014-04-29 11:47:07
考虑到可读性,我将把我的解决方案(基于stu.salsbury的anwser)放在这里。
将此代码添加到应用程序的抽象模板中,这样它就可以在每个页面上运行。
$rootScope.previousState;
$rootScope.currentState;
$rootScope.$on('$stateChangeSuccess', function(ev, to, toParams, from, fromParams) {
$rootScope.previousState = from.name;
$rootScope.currentState = to.name;
console.log('Previous state:'+$rootScope.previousState)
console.log('Current state:'+$rootScope.currentState)
});
跟踪rootScope中的更改。这很方便。
https://stackoverflow.com/questions/16635381
复制相似问题