作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我有一个带有 Sails JS 后端的 Angular JS 应用程序,在路由内部(在 app.js 中)我有:
.state('app.detail', {
url: "/detail",
views: {
'menuContent' :{
templateUrl: "templates/detail.html",
controller: 'UserUpdateCtrl',
resolve: {
auth: ["$q", "userData", function($q, userData) {
var userInfo = userData.getUserInfo();
if (userInfo) {
return $q.when(userInfo);
} else {
return $q.reject({ authenticated: false });
}
}]
},
}
}
})
(这是继 this guide )
现在在同一个文件上,我有 $routeChangeError:
.run(function($rootScope) {
$rootScope.$on("$routeChangeError", function(event, current, previous, eventObj) {
if (eventObj.authenticated === false) {
$location.path("/login");
}
});
在 chrome 上调试时,我看到函数已定义,但未被调用。
我在这里缺少什么?
最佳答案
好吧,假设您使用的是 Angular UI 路由器,我相信您只是错误地翻译了官方的错误处理程序。
来自docs for $state
:
Fired when an error occurs during transition. It's important to note that if you have any errors in your resolve functions (javascript errors, non-existent services, etc) they will not throw traditionally. You must listen for this $stateChangeError event to catch ALL errors.
并且这个处理程序的参数不同,尝试这样的事情:
$rootScope.$on("$stateChangeError", function(event, toState, toParams, fromState, fromParams, error) {
if (error && !error.authenticated) {
$location.path("/login");
}
});
下面是参数的描述,重要的加粗:
关于javascript - $routeChangeError 未在 $q.reject 上调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24604931/
我是一名优秀的程序员,十分优秀!