$rootScope.$on('$stateChangeStart', function(event, toState , toParams, fromState, fromParams)
{
if(toState.params.isRestricted === true && !$rootScope.isAppLog ) {
event.preventDefault();
}
// here I want value returned by the detectBackOrForward() onhashchange that exwcutes on back or forward button click
});
var detectBackOrForward = function(onBack, onForward) {
hashHistory = [window.location.hash];
historyLength = window.history.length;
return function() {
var hash = window.location.hash, length = window.history.length;
if (hashHistory.length && historyLength == length) {
if (hashHistory[hashHistory.length - 2] == hash) {
hashHistory = hashHistory.slice(0, -1);
onBack();
} else {
hashHistory.push(hash);
onForward();
}
} else {
hashHistory.push(hash);
historyLength = length;
}
}
};
window.addEventListener("hashchange", detectBackOrForward(
function() { return "back" },
function() { return "forward" }
));
在上面的代码中,当我分别点击浏览器向后和转发按钮时,我成功返回了"back"
和"forward"
值。但是我无法在$stateChangeStart
事件中使用返回的值。请帮我解决这个问题。