我正在使用 ui-router 和 ui-router extras
$scope.$on('$transitionStart', function (e, $transition$) {
var params = {id:1};
factory.webService(params)
.success(function (data) {
if (data.d == "Expired") {//Inspects the application session
//Stops the state request and sents you to the login screen
e.preventDefault();
location.href = "login.html";
}
else if (data.d == "NotAllowed") {
//Stops the state request and keeps you on the state you already are
e.preventDefault();
} else {
//Proceed to load the requested state
}
})
.error(function (data, status) {
alert(data.Message);
});
});
我需要在 $stateChangeStart 加载之前解决成功部分并且无法弄清楚如何去做。
有没有办法做到这一点?
编辑
所以我有这样的代码
.state('myState', {
url: "/myState",
templateUrl: 'views/template.html',
controller: 'ctrlTemplate',
viewId: 99,
resolve: {
fn: function ($stateParams, myFactory) {
myFactory.checkSession({viewId:99})
.then(function onSuccess(response) {
var data = response.data;
if (data.d == "Expired") {
throw "Expired";
}
else if (data.d == "NotAllowed") {
throw "NotAllowed";
} else {
//RETURN to chain data
return data;
//Proceed to load the requested state
}
})
.catch(function (response) {
//e.preventDefault();
});
}
}
})
$http.then
函数仍在解析之后发生$stateChangeStart
并且$transitionStart
新状态已经加载。我可以在浏览器控制台调试器中看到它发生
请帮忙。