浏览器历史:跳过抽象状态
Browser History: skip abstract state
浏览器后退按钮导航按预期工作,但在遇到抽象状态时,它会解析为原始状态(这必然是导航到的抽象状态的子状态)。
在 angular-ui-router 中有没有办法抑制抽象状态被添加到历史中?
遇到同样的问题。希望对你有所帮助How do I get the Back Button to work with an AngularJS ui-router state machine?
对我来说有点难,看来必须有一个更简单的解决方案
编辑:
我做到了。
首先你需要你的旧状态(我们也有本地存储的包装器,所以你应该改变方法)。
angular
.module('app')
.run(appRunFunction);
appRunFunction.$inject = ['$rootScope', 'localstorage'];
function appRunFunction($rootScope, localstorage) {
$rootScope.$on('$stateChangeSuccess', function(event, toState, toParam, fromState, fromParam) {
localstorage.setValue('lastState', fromState.name);
});
};
我们还编写了一项服务,可在将您的下一个常规状态与上一个常规状态进行比较后从抽象状态重定向到常规状态
angular
.module('app.services')
.factory('states', states);
states.$inject = ['$state', 'localstorage'];
function states($state, localstorage) {
var service = {reloadWithParam: reloadWithParam};
return service;
function reloadWithParam(param, value) {
var fromState = localstorage.getValue('lastState') + '.' + localstorage.getValue('lastPage');
var toState = $state.current.name + '.' + value;
var paramObject = {};
paramObject[param] = value;
if (fromState === toState) {
window.history.go(-1);
} else {
$state.go($state.current, paramObject, { notify: false});
localstorage.setValue('lastPage', value);
}
}
}
浏览器后退按钮导航按预期工作,但在遇到抽象状态时,它会解析为原始状态(这必然是导航到的抽象状态的子状态)。
在 angular-ui-router 中有没有办法抑制抽象状态被添加到历史中?
遇到同样的问题。希望对你有所帮助How do I get the Back Button to work with an AngularJS ui-router state machine?
对我来说有点难,看来必须有一个更简单的解决方案
编辑: 我做到了。 首先你需要你的旧状态(我们也有本地存储的包装器,所以你应该改变方法)。
angular
.module('app')
.run(appRunFunction);
appRunFunction.$inject = ['$rootScope', 'localstorage'];
function appRunFunction($rootScope, localstorage) {
$rootScope.$on('$stateChangeSuccess', function(event, toState, toParam, fromState, fromParam) {
localstorage.setValue('lastState', fromState.name);
});
};
我们还编写了一项服务,可在将您的下一个常规状态与上一个常规状态进行比较后从抽象状态重定向到常规状态
angular
.module('app.services')
.factory('states', states);
states.$inject = ['$state', 'localstorage'];
function states($state, localstorage) {
var service = {reloadWithParam: reloadWithParam};
return service;
function reloadWithParam(param, value) {
var fromState = localstorage.getValue('lastState') + '.' + localstorage.getValue('lastPage');
var toState = $state.current.name + '.' + value;
var paramObject = {};
paramObject[param] = value;
if (fromState === toState) {
window.history.go(-1);
} else {
$state.go($state.current, paramObject, { notify: false});
localstorage.setValue('lastPage', value);
}
}
}